PHP将图像裁剪为方形拇指并显示黑色条纹

时间:2015-03-12 15:05:14

标签: php image crop

我知道这个主题似乎经常出现在这个网站上,但似乎没有人能解决这个问题。我确实有一个脚本可以重新调整图像,将其显示为大小为200x200px的缩略图。正确地重新调整图像,但是当您查看输出图像时,您可以看到拇指的实际尺寸与原始尺寸相同,黑色条纹填充其余空间。是否有任何想法可以防止这种情况并真实地拍摄图像?

foreach( $images as $image ) {
$dn = dirname($image);
$thumbsDir = $dn; // path to the thumbnails destination directory

$imageName = "thumb.jpg"; // returns "cheeta.jpg"
$thumbnail = $thumbsDir."/".$imageName; // thumbnail full path and name, i.e "./gallery/thumbs/cheeta.jpg"
// for each image, get width and height
$imageSize = getimagesize( $image ); // image size 
$imageWidth = $imageSize[0];  // extract image width 
$imageHeight = $imageSize[1]; // extract image height
// set the thumb size
if( $imageHeight > $imageWidth ){
// images is portrait so set thumbnail width to 100px and calculate height keeping aspect ratio
$thumbWidth = 200;
$thumbHeight = floor( $imageHeight * ( 200 / $imageWidth ) );           
$thumbPosition  = "margin-top: -" . floor( ( $thumbHeight - 200 ) / 2 ) . "px; margin-left: 0";
} else {
// image is landscape so set thumbnail height to 100px and calculate width keeping aspect ratio
$thumbHeight = 200;
$thumbWidth = floor( $imageWidth * ( 200 / $imageHeight ) ); 
$thumbPosition  = "margin-top: 0; margin-left: -" . floor( ( $thumbWidth - 200 ) / 2 ) . "px";
} // END else if
// verify if thumbnail exists, otherwise create it
if ( !file_exists( $thumbnail ) ){
$createFromjpeg = imagecreatefromjpeg( $image );
$thumb_temp = imagecreatetruecolor( $thumbWidth, $thumbHeight );
imagecopyresampled( $thumb_temp, $createFromjpeg, 0 - ($thumbWidth - 200) / 2, 00 - ($thumbHeight - 200) / 2, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight );
imagejpeg( $thumb_temp, $thumbnail, 80 );
} // END if()

感谢您的帮助!

将链接图片视为参考 http://i.stack.imgur.com/U743p.jpg

0 个答案:

没有答案