【问题标题】:codeigniter image thumbnail custom sizescodeigniter 图像缩略图自定义大小
【发布时间】:2016-10-24 20:25:48
【问题描述】:

我是 PHP 新手,我需要有关此功能的帮助,我想使用 Width: 300px 和 height: auto (纵横比) 我希望它不裁剪图像只调整大小 这是函数

if ( ! function_exists('create_rect_thumb'))
{

function create_rect_thumb($img,$dest,$ratio=3)
{

    $seg = explode('.',$img);   //explde the source to get the image extension
    $thumbType    = 'jpg';      //generated thumb will be of type jpg
    $thumbPath    = $dest;  //destination path of the thumb -- original image name will be appended
    $thumbQuality = 90;             //quality of the thumbnail (in percent)

    //chech the image type and create image accordingly
    if($seg[2]=='jpg' || $seg[2]=='JPG' || $seg[2]=='jpeg')
    {
        if (!$full = imagecreatefromjpeg($img)) {
            return 'error';
        }
    }
    else if($seg[2]=='png')
    {
        if (!$full = imagecreatefrompng($img)) {
            return 'error';
        }           
    }
    else if($seg[2]=='gif')
    {
        if (!$full = imagecreatefromgif($img)) {
            return 'error';
        }           
    }

    $width  = imagesx($full);
    $height = imagesy($full);

    /*wourk out the thumbnail size*/
    $resizedHeight  = min($width*$ratio/8,$height);
    $resizedWidth   = $width;

    /* work out starting point */
    $thumbx = 0;    // x always starts at zero -- the thumbnail gets the same width as the source image
    $extra_height = $height - $resizedHeight;
    $thumby = floor(($extra_height) / 2);

    /* create the small smaller version, then crop it centrally to create the thumbnail */
    $resized  = imagecreatetruecolor($resizedWidth, $resizedHeight);
    imagecopy($resized, $full,0,0,$thumbx,$thumby,$resizedWidth,$resizedHeight);

    $name = name_from_url($img);

    imagejpeg($resized, $thumbPath.str_replace('_large.jpg', '_thumb.jpg', $name), $thumbQuality);
}

}

现在它正在创建具有原始图像全宽和原始图像一半高度的图像横幅

【问题讨论】:

    标签: php codeigniter thumbnails


    【解决方案1】:

    使用 ci 图像处理类

    http://www.codeigniter.com/user_guide/libraries/image_lib.html

    您可以使用此代码

    $this->load->library('image_lib');

    $config['image_library'] = 'gd2';

    $config['source_image'] = '/path/to/image/mypic.jpg';

    $config['create_thumb'] = TRUE;

    $config['maintain_ratio'] = TRUE;

    $config['width'] = 300;

    $config['height'] = 50;

    $this->load->library('image_lib', $config);

    $this->image_lib->resize();

    如果你使用 ci 文件上传类?然后您可以获得图像完整路径 使用 $this->upload->data('upload_path');这个函数 您的图像存储位置并将其放入 $config['source_image']

    示例: $config['source_image'] = $this->upload->data('upload_path');

    【讨论】:

    • 嗨,谢谢你的回答,但正如我所说,我是 PHP 编程语言的新手,我不知道如何实现它,所以我请好心人帮忙我通过以正确的方式编辑这个函数,所以我可以在我的代码中替换它
    猜你喜欢
    • 2014-07-24
    • 2014-10-04
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2015-03-17
    相关资源
    最近更新 更多