[英]codeigniter image thumbnail custom sizes
我是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);
}
}
現在,它正在創建全幅為原始圖像,半高為原始圖像的圖像橫幅
使用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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.