如何在php中按比例调整图像大小

时间:2013-12-24 02:23:50

标签: php

我想按比例调整图像大小为500px X 500px我从网上找到了下面的代码然而文章没有如何使用脚本的例子,有人可以让我开始如何显示调整大小的图像?

$filename = 'test.png';
list($width, $height, $type, $attr) = getimagesize($filename);

$orig_w = $width;
$orig_h = $height;
$MIN_W = 500;
$MIN_H = 500;
$MAX_H = 500;
$MAX_W = 500;

function image_resize_up($orig_w, $orig_h, $MIN_W, $MIN_H){
$ratio = $orig_w * 1.0 / $orig_h;   
$w_undersized = ($orig_w < $MIN_W);
$h_undersized = ($orig_h < $MIN_H);

if ($w_undersized OR $h_undersized)
{
      $new_w = round( max($MIN_W, $ratio * $MIN_H) );
      $new_h = round( max($MIN_H, $MIN_W / $ratio) );
      return array('width' => $new_w, 'height' => $new_h);
}
return null;
}

function image_resize_down($orig_w, $orig_h, $MAX_W, $MAX_H){
$ratio = $orig_w * 1.0 / $orig_h;

$w_undersized = ($orig_w > $MAX_W);
$h_undersized = ($orig_h > $MAX_H);

if ($w_undersized OR $h_undersized)
{
      $new_w = round( min($MAX_W, $ratio * $MAX_H) );
      $new_h = round( min($MAX_H, $MAX_W / $ratio) );
      return array('width' => $new_w, 'height' => $new_h);
}
return null;
}
echo $w_undersized;

我试过

echo "<img src=".$filename.$new_w.$new_h.">";//gives me undefined variable and the image isn't resized.

4 个答案:

答案 0 :(得分:1)

您错误地定义了您的属性,请尝试:

echo "<img src='".$filename."' style='width:".$new_w."; height:".$new_h."; '>";

祝你好运

答案 1 :(得分:1)

首先,你需要一个功能来裁剪图像,使宽度和高度相等:

function crop_image_square($filename, $_filename){
list($width, $height, $type) = getimagesize($filename);
$min = min($width, $height);
$x = ($width > $height) ? ($width-$height)/2 : 0;
$y = ($width < $height) ? ($height-$width)/2 : 0;
    switch($type){
    case IMG_GIF:    $src = imagecreatefromgif($filename);     break;
    case IMG_JPG:    $src = imagecreatefromjpeg($filename);    break;
    case IMG_PNG:    $src = imagecreatefrompng($filename);     break;
    // and so on...
    }
$dest = imagecreatetruecolor($min, $min);
imagecopy($dest, $src, 0, 0, $x, $y, $width, $height);
    switch($type){
    case IMG_GIF:    imagegif($dest, $_filename);     break;
    case IMG_JPG:    imagejpeg($dest, $_filename);    break;
    case IMG_PNG:    imagepng($dest, $_filename);     break;
    // and so on...
    }
imagedestroy($src);
imagedestroy($dest);
}

然后您可以将该功能应用于一个或多个图像:

crop_image_square("image_01.jpg", "image_01_cropped.jpg");
crop_image_square("image_02.jpg", "image_02_cropped.jpg");
crop_image_square("image_03.jpg", "image_03_cropped.jpg");

PHP部分已完成。正如您所指出的那样,使用纯HTML可以轻松地显示调整大小的版本:

<img src="image_01_cropped.jpg" width="500" height="500"/>
<img src="image_02_cropped.jpg" width="500" height="500"/>
<img src="image_03_cropped.jpg" width="500" height="500"/>

答案 2 :(得分:1)

在PHP / GD中处理图像可能很乏味,这就是为什么我编写SimpleImage库以减少事情的原因。它解决了GD图像处理带来的许多边缘情况和陷阱。

以下是如何按比例调整图像大小以适应500x500的方框:

// Load the image from image.jpg
$image = new \claviska\SimpleImage('image.jpg');

// Proportionally resize to fit inside a 500x500 box and save to result.jpg
$image->bestFit(500, 500)->toFile('result.jpg');

SimpleImage可以通过Composer安装或手动链接。 Details in the readme.

答案 3 :(得分:0)

如果您愿意,可以使用库SimpleImage

include('src/abeautifulsite/SimpleImage.php');  //Load the SimpleImage
$image = new SimpleImage();  //Create an instance.
$image->load('picture.jpg'); //Load the picture.jpg.
image->resize(500, 500);      //Resize.
$image->save('picture2.jpg');//Save as picture2.jpg.