问题描述
|
嗨,我想在细节部分显示时调整图像大小。为此,我检查了许多PHP脚本,但在我的语言环境中不起作用。一些显示图像路径的脚本必须以/开头,但是当我给出类似的脚本时,找不到显示图像的路径。同样,大多数脚本演示页面都显示伪造图像。
解决方法
最好的选择是在上传图片时裁剪图片。请找到以下使用GD库进行图像裁剪的代码,可能对您有帮助。
<?php
function createThumb($upfile,$dstfile,$max_width,$max_height){
$size = getimagesize($upfile);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height)) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if($size[\'mime\'] == \"image/jpeg\"){
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width,$tn_height);
imagecopyresampled($dst,$src,$tn_width,$tn_height,$width,$height);
imageinterlace( $dst,true);
ImageJpeg($dst,100);
} else if ($size[\'mime\'] == \"image/png\"){
$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width,$height);
Imagepng($dst,$dstfile);
} else {
$src = ImageCreateFromGif($upfile);
$dst = ImageCreateTrueColor($tn_width,$height);
imagegif($dst,$dstfile);
}
}
//usage
if(isset($_FILES[\'upload_Image\'][\'name\']) && $_FILES[\'upload_Image\'][\'name\']!==\'\') {
$ext = substr($_FILES[\'upload_Image\'][\'name\'],strpos($_FILES[\'upload_Image\'][\'name\'],\'.\'),strlen($_FILES[\'upload_Image\'][\'name\'])-1);
$imgNormal = time().$ext;
$normalDestination = \"Photos/Orignal/\" . $imgNormal;
$httpRootLarge = \"Photos/Large/\" . $imgNormal;
$httpRootSmall = \"Photos/Small/\" . $imgNormal;
$httpRootThumb = \"Photos/Thumb/\" . $imgNormal;
move_uploaded_file($_FILES[\'upload_Image\'][\'tmp_name\'],$normalDestination);
createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image
createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image
createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image
}
?>
, 有两种选择:
创建图像的缩略图(通过PHP中的图形库)并存储(更好地提高性能)。
使用PHP图形库动态调整图像大小并显示它。
您也可以参考此页面以获取更多详细信息:http://php.net/manual/book.image.php
, 我认为您需要ImageMagick。您可以在Google中找到很多示例,我只有Perl示例。
, 伙计们组成的nettut在这个主题上有很棒的经验:使用PHP轻松调整图像大小
:)