【问题标题】:Auto Resize the image in PHP在 PHP 中自动调整图像大小
【发布时间】:2014-07-03 11:56:54
【问题描述】:

我想在上传时自动调整图像大小。我想知道很多网站,但在每个网站中,用户都为所有图像设置了新宽度和新高度,但我想自动调整大小,因为问题是用户何时上传相同的尺寸图像然后我们将很容易剪切,但是当用户上传横向或纵向尺寸图像时,图像会弄乱并剪切错误的尺寸。所以我面临这个问题。

【问题讨论】:

  • PHP Thumbnail Image Resizing with proportions 的可能重复项(但 $maxwidth$maxheight 不同)(我会投赞成票,但我收到一个 javascript 错误:@987654324 @)
  • 显示一些代码来显示您遇到问题的地方怎么样?

标签: php file-upload image-resizing autoresize


【解决方案1】:
In auto resize case this may be helpful.i am using this code in my oproject 

list($originalWidth, $originalHeight) = getimagesize($imageFile);
$ratio = $originalWidth / $originalHeight;


$targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));

if ($ratio < 1) {
    $targetWidth = $targetHeight * $ratio;
} else {
    $targetHeight = $targetWidth / $ratio;
}

$srcWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcX = $srcY = 0;


$targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

if ($ratio < 1) {
    $srcX = 0;
    $srcY = ($originalHeight / 2) - ($originalWidth / 2);
    $srcWidth = $srcHeight = $originalWidth;
} else {
    $srcY = 0;
    $srcX = ($originalWidth / 2) - ($originalHeight / 2);
    $srcWidth = $srcHeight = $originalHeight;
}

$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);

【讨论】:

  • $newwidth=120; /// 定义新的宽度 $newheight=120; ///定义新高度我不想定义高度和宽度。我想自动调整大小
【解决方案2】:

您应该使用这些教程,但您自己计算其他值。例如,您有一个宽度为 600 像素、高度为 1200 像素的图像;你希望它是 200px 宽度。计算如下:

$newImageWidth = 200;
$imageWidth = 600;
$imageHeight = 1200;
$ratio = $imageHeight / $imageWidth;
$newImageHeight = $newImageWidth * ratio;

【讨论】:

  • 感谢您的回复,但如果图像尺寸为 2000*500,则会再次出现相同的问题,然后图像将再次切割成错误的尺寸。
  • 为什么?在这种情况下,它将调整为 200x50。你想得到什么尺寸?
  • 如果您想展示示例,请查看此链接webappers.com/2008/03/14/… 在此链接中,您正在展示一张咖啡图片。我想要那样
  • 什么意思? - 如果你有第一张图片作为上传,你可以在你的代码中输入$newImageWidth = 100; 之类的东西,其余的应该可以工作。您应该计算出正确的高度。
  • 任何维度,例如用户上传 2000*50 , 500*1000,200*200,1024*900 所有维度。图片将完美调整大小
猜你喜欢
  • 2017-04-29
  • 1970-01-01
  • 2011-02-18
  • 2013-01-16
  • 2014-08-07
  • 1970-01-01
相关资源
最近更新 更多