내가 실행하는 웹 사이트의 경우 사용자가 그린 그림을 갤러리에 업로드 할 수 있습니다. 다른 사용자에게 표시 할 이미지의 작은 이미지와 작은 이미지를 만듭니다 (작은 이미지를 클릭하면 전체 크기의 이미지가 표시됨).PHP에서 큰 이미지의 크기를 올바르게 조정하는 방법
이 점을 고려하여 매우 간단한 크기 조정 스크립트를 만들었습니다. 대부분의 경우이 스크립트는 완벽하게 작동합니다. 그러나 스크립트가 완전히 망가지는 이상한 경우가 있습니다.
(최대 너비 또는 높이 150의 400의 또 다른 하나의 썸네일을 생성) 스크립트를 통해 파일 http://img191.imageshack.us/img191/2268/935full.png (1641x3121) 우리는 완벽한 축소판 http://img267.imageshack.us/img267/5803/935thumb.png (78x150)를 얻을 작은 뷰 이미지가 제대로 크기를 실행하는 경우, 그러나 잘라내어 뻗은 http://img28.imageshack.us/img28/4002/935show.png (211 x 400).
저의 마음에, 제 질문은 : PHP 또는 논리 오류에 문제가 있습니까? 어떻게 해결할 수 있습니까?
감사합니다. 이러한 미리보기 이미지를 만드는 데 사용하는 코드는 다음과 같습니다.
<?php
/**
* Creates a thumbnail for any type of pre-existing image. Always saves as PNG image
*
* @param string - The location of the pre-existing image.
* @param string - The location to save the thumbnail, including filename and extension.
* @param int - The Maximum Width, Default of 150
* @param int - The Maximum Height, Default of 150
* @return bool - Success of saving the thumbnail.
*/
function imagecreatethumbnail($file,$output,$max_width = 150,$max_height = 150)
{
$img = imagecreatefromstring(file_get_contents($file));
list($width, $height, $type, $attr) = getimagesize($file);
if($height > $max_height || $width > $max_width)
{
if($width > $height)
{
$thumb_width = $max_width;
$thumb_height = ceil(($height * $thumb_width)/$width);
}
else
{
$thumb_height = $max_height;
$thumb_width = ceil(($width * $thumb_height)/$height);
}
} else {
$thumb_width = $width;
$thumb_height = $height;
}
imagesavealpha($img,true);
$thumb = imagecreatetruecolor($thumb_width,$thumb_height);
imagesavealpha($thumb,true);
imagealphablending($thumb,false);
imagecopyresampled($thumb,$img,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
$return = imagepng($thumb,$output);
imagedestroy($img);
imagedestroy($thumb);
return $return;
}
스크립트가 인터럽트 (max_execution_time 또는 memory_limit)로 인해 발생합니까? – JochenJung
아니요, 완전히 실행됩니다. – Navarr
'imagecreatethumbnail ("http://img191.imageshack.us/img191/2268/935full.png", "m.png", 400, 400);으로 코드를 실행하면 올바른 이미지가 생성됩니다. PHP 5.2.6-1 + lenny8. PHP의 GD 버전은 5.2.6.dfsg.1-1 + lenny8입니다. – jmz