2009-09-30 38 views
1

这里是网站的IM谈论 http://makeupbyarpi.com/portfolio.phpPHP GD:当我通过PHP裁剪图像的一些图像出来smushed

,你会发现一些图像都smushed宽度方向。

我使用的代码是这样的:

$width="500"; 
$height="636"; 


$img_src = $_FILES['galleryimg']['tmp_name']; 
$thumb = "../gallery/".rand(0,100000).".jpg"; 


//Create image stream 
$image = imagecreatefromjpeg($img_src); 

//Gather and store the width and height 
list($image_width, $image_height) = getimagesize($img_src); 


//Resample/resize the image 
$tmp_img = imagecreatetruecolor($width, $height); 
imagecopyresampled($tmp_img, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height); 

//Attempt to save the new thumbnail 
if(is_writeable(dirname($thumb))){ 
    imagejpeg($tmp_img, $thumb, 100); 

} 

//Free memory 
imagedestroy($tmp_img); 
imagedestroy($image); 

是那些获得上传的图片是巨大的,有时3000px由2000像素和我有PHP的作物下来,以500×536和一些基于景观图像得到smushed。有没有一个公式我可以用它仔细裁剪,使图像出来好?

感谢

回答

2

你可以调整大小,如果需要添加letterbox。你只需要调整宽度,然后计算新的高度(假设宽高比与原来的比例相同),那么如果高度不等于首选高度,则需要绘制黑色矩形(封面背景),然后居中图片。

你也可以做一个邮筒,但是除了宽度变成高度和高度变成宽度之外,你可以做同样的事情。

编辑:其实,你调整最大的那个,如果宽度较大,你调整大小,如果高度较大,那么你调整大小。取决于你调整哪一个,你的脚本应该是信箱或邮箱。

编辑2:

<?php 
    // Define image to resize 
    $img_src = $_FILES['galleryimg']['tmp_name']; 
    $thumb = "../gallery/" . rand(0,100000) . ".jpg"; 

    // Define resize width and height 
    $width = 500; 
    $height = 636; 

    // Open image 
    $img = imagecreatefromjpeg($img_src); 

    // Store image width and height 
    list($img_width, $img_height) = getimagesize($img_src); 

    // Create the new image 
    $new_img = imagecreatetruecolor($width, $height); 

    // Calculate stuff and resize image accordingly 
    if (($width/$img_width) < ($height/$img_height)) { 
     $new_width = $width; 
     $new_height = ($width/$img_width) * $img_height; 
     $new_x = 0; 
     $new_y = ($height - $new_height)/2; 
    } else { 
     $new_width = ($height/$img_height) * $img_width; 
     $new_height = $height; 
     $new_x = ($width - $new_width)/2; 
     $new_y = 0; 
    } 

    imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0, $new_width, $new_height, $img_width, $img_height); 

    // Save thumbnail 
    if (is_writeable(dirname($thumb))) { 
     imagejpeg($new_img, $thumb, 100); 
    } 

    // Free up resources 
    imagedestroy($new_img); 
    imagedestroy($img); 
?> 

抱歉过了好一会儿,我碰到在计算的一个小错误,这我无法修复像10分钟= /这应该工作跑了。

+0

所以如果宽度大于这种情况下的高度,我将宽度设置为500,并保持高度? – iaddesign 2009-09-30 17:45:44

+0

给我5分钟我会写一个例子。 – 2009-09-30 17:47:19

+0

哇,这很不错。非常感谢!你不需要为此付出代价,只要你对我产生了很大的帮助。我希望我能把你的观点:) – iaddesign 2009-10-01 01:11:54