【发布时间】: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