codeigniter图像缩略图自定义尺寸

时间:2016-06-22 18:31:33

标签: php codeigniter thumbnails

我是非常新的PHP,我需要这个功能的帮助,我想使用宽度:300px和高度:自动(宽高比) 我希望它不裁剪图像只调整大小 这是函数

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);
}

}

现在它正在创建全宽图像横幅作为原始图像和原始图像的半高

1 个答案:

答案 0 :(得分:0)

使用ci Image Manipulation Class

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

您可以使用此代码

$这 - >负载>库( 'image_lib');

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

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

$ config ['create_thumb'] = TRUE;

$ config ['maintain_ratio'] = TRUE;

$ config ['width'] = 300;

$ config ['height'] = 50;

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

$这 - > image_lib->调整大小();

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

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