【发布时间】:2011-09-20 08:56:24
【问题描述】:
我想将上传的图片调整为宽度:180px,高度成比例。是否有任何课程可以做到这一点?
感谢您的帮助!
【问题讨论】:
我想将上传的图片调整为宽度:180px,高度成比例。是否有任何课程可以做到这一点?
感谢您的帮助!
【问题讨论】:
我认为这个问题可以使用带有实际代码示例的答案。下面的代码向您展示了如何调整目录uploaded 中的图像大小,并将调整后的图像保存在文件夹resized 中。
<?php
// the file
$filename = 'uploaded/my_image.jpg';
// the desired width of the image
$width = 180;
// content type
header('Content-Type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
$height = $width/$ratio_orig;
// resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// output
imagejpeg($image_p, 'resized/my_image.jpg', 80);
?>
【讨论】:
首先你需要获取当前的图片尺寸:
$width = imagesx($image);
$height = imagesy($image);
然后计算缩放因子:
$scalingFactor = $newImageWidth / $width;
当具有缩放因子时,只需计算图像的新高度:
$newImageHeight = $height * $scalingFactor;
然后创建新图像;
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
可能这些 sn-ps 会有所帮助:
http://www.codeslices.net/snippets/resize-scale-image-proportionally-to-given-width-in-phphttp://www.codeslices.net/snippets/resize-scale-image-proportionally-in-php
至少他们为我工作。
【讨论】:
您可以使用 imagecopyresampled php 函数。您还可以计算新的尺寸。
【讨论】:
用户 jquery 插件 JCrop,并设置图像的纵横比... 检查此链接以获取详细信息: http://www.webresourcesdepot.com/jquery-image-crop-plugin-jcrop/
【讨论】: