php - Square thumbnail advance -
i have php thumbnail function. how works can check below:
public static function makethumb($source, $destination, $thumb_width){ $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $x = 0; $y = 0; $status = false; if ($width > $height) { $x = ceil(($width - $height) / 2); $width = $height; } else if ($height > $width) { $y = ceil(($height - $width) / 2); $height = $width; } $new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die ('cannot initialize new gd image stream'); $extension = self::get_file_extension($source); if ($extension == 'jpg' || $extension == 'jpeg') $image = imagecreatefromjpeg($source); if ($extension == 'gif') $image = imagecreatefromgif($source); if ($extension == 'png') $image = imagecreatefrompng($source); imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height); if ($extension == 'jpg' || $extension == 'jpeg') $status = @imagejpeg($new_image, $destination); if ($extension == 'gif') $status = @imagegif($new_image, $destination); if ($extension == 'png') $status = @imagepng($new_image, $destination); imagedestroy($image); return $status; }
please check images below (how works):
question: how can image result (this thumb photoshop)?
the use of wrong thumbwidth (and no thumbheight!) gives square result.
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
according php manual (http://php.net/manual/en/function.imagecopyresampled.php), imagecopyresampled copies resized region of original image. want whole original. do
imagecopyresampled($new_image,$image,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
you need calculate $thumbheight first - height*thumbwidth/width do, or divide original width , height set amount.
as comments below show, parameters used in function rather confusing. add explanation in hope can create like.
imagecopyresampled
copies , resizes rectangular part of image rectangular part of new one. parameters needed twice on - original , new image. drawing might help:
$image $new_image +----------------+ +----------------+ | source img | | destination img| | | | | | | | +--------+ | | +------+| | | part | | | | part || | | copy | | | | || | +--------+ | | +------+| | | +----------------+ +----------------+
the arguments are
$dst_image new image (dst = destination) $src_image old image (src = source) $dst_x , $dst_y top left of destination area $src_x , $src_y top left of source area $dst_w , $dst_h width , height of destination area $src_w , $src_h width , height of source area
if you're trying copy whole source image, new size, in new destination image, like
$dst_image new image (dst = destination) $src_image old image (src = source) 0 , 0 top left of destination area 0 , 0 top left of source area $dst_w , $dst_h new width , height of thumb $width , $height width , height of whole source image
but i'm not size want thumbnail, have leave find correct values.
Comments
Post a Comment