【问题标题】:How change the size of an uploaded image in codeigniter without creating a thump image?如何在不创建重击图像的情况下更改 codeigniter 中上传图像的大小?
【发布时间】:2016-02-12 14:38:03
【问题描述】:

我们可以为上传的图片生成不同的重击。 这使得该图像的几个副本。 我只想上传一张图片。 但在视图中,它必须提供不同的尺寸。 有人知道怎么做吗?

【问题讨论】:

  • 创建图像缩略图的优点是无需传输图像供用户选择他/她想要详细查看的图像。您可以在请求图像时动态地对其进行临时转换并将该图像发送出去。但是,使用这种方法,每次请求图像时,您都会浪费时间进行转换。
  • 显示到目前为止您尝试过的代码。在此之前,您还可以阅读this article 了解如何提问。
  • 你好 hherger,你告诉创造重击是最好的方法吗?图像文件夹的大小将增加。
  • @hherger 想说的是,您可以决定是保存缩略图还是只保存一张图像并在显示时将其转换为所需的大小。这两种选择各有利弊。
  • Cool Acid@ 在您看来哪种方法最好?

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

使用名为phpThumb() 的PHP 库。 phpThumb() 使用 GD 库从图像动态创建缩略图。

您可以动态创建图像缩略图

<?php

function image_thumb( $image_path, $height, $width )
 {
    $CI =& get_instance();

    // Path to image thumbnail
    $image_thumb = dirname( $image_path ) . '/' . $height . '_' . $width . '.jpg';

    if ( !file_exists( $image_thumb ) ) {
        // LOAD LIBRARY
        $CI->load->library( 'image_lib' );

        // CONFIGURE IMAGE LIBRARY
        $config['image_library']    = 'gd2';
        $config['source_image']     = $image_path;
        $config['new_image']        = $image_thumb;
        $config['maintain_ratio']   = TRUE;
        $config['height']           = $height;
        $config['width']            = $width;
        $CI->image_lib->initialize( $config );
        $CI->image_lib->resize();
        $CI->image_lib->clear();
    }

    return '<img src="' . dirname( $_SERVER['SCRIPT_NAME'] ) . '/' . $image_thumb . '" />';
}

在视图中的使用:

echo image_thumb( 'assets/images/picture-1/picture-1.jpg', 50, 50 ); 

【讨论】:

    猜你喜欢
    • 2016-05-04
    • 2014-04-07
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多