如何通过PHP脚本调整图片大小

6

我正在创建一款应用程序,需要通过php脚本将大图转换为缩略图,并将其编码为base64,以便通过json发送到我的android应用程序。

我在调整图像大小方面遇到了问题。我需要一个帮助我完成这个操作的php脚本。


2
你正在使用哪个图像处理库? - San
7个回答

7
您可以尝试使用这个图像调整大小的教程
此外,您还可以使用这段代码进行调整大小功能(GD)。
<?php

$thumb = new Imagick();
$thumb->readImage('myimage.gif');    $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy(); 

?>

Or, a shorter version of the same:

<?php

$thumb = new Imagick('myimage.gif');

$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');

$thumb->destroy(); 

?>

并且还可以参考以下链接调整图像大小:

1.YETANOTHERLinks

2.9Lession

此外,将Base64转换为图像,请参考此链接Links


2
您也可以使用$this->cropThumbnailImage代替resizeImage来避免缩略图的拉伸。 - Flimm

5
下面的代码创建了一个名为createThumbs的函数,该函数将获取三个参数。第一个和第二个分别是包含原始图像的目录的路径和缩略图将放置在其中的目录的路径。第三个参数是您想要缩略图图像的宽度。
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' ) 
    {
      echo "Creating thumbnail for {$fname} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
}
// call createThumb function and pass to it as parameters the path 
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width. 
// We are assuming that the path will be a relative path working 
// both in the filesystem, and through the web for links
createThumbs("upload/","upload/thumbs/",100);
?>

1
您可以尝试使用imagick - http://php.net/manual/en/imagick.resizeimage.php 来调整图像大小。示例代码: 创建缩略图:
<?php

$thumb = new Imagick();
$thumb->readImage('myimage.gif');    
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy(); 

?>

1
如果您正在使用GD,代码将如下所示:
<?php
// File and new size
$filename = 'test.jpg';

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = YOUR REQUIRED WIDTH;
$newheight = YOUR REQUIRED HEIGHT;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

1

0

尝试使用php LANCZOS可能会更快

$thumb = new Imagick();

$thumb->readImage('XYZ.jpg');

$thumb->resizeImage(640,360,Imagick::FILTER_LANCZOS,1);

$thumb->writeImage('XYZ2.jpg');

$thumb->clear();
$thumb->destroy();

Or Shorter Version ::

$thumb = new Imagick('XYZ.jpg');

$thumb->resizeImage(640,360,Imagick::FILTER_LANCZOS,1);

$thumb->writeImage('XYZ2.jpg');

$thumb->destroy();

0

这篇文章有点旧了,但如果有人需要,我刚刚写了这个函数

function save_image_from_64($path, $name, $dimension, $original)
{
    header("Content-Type: image/jpeg");
    list($width,$height) = explode('x',$dimension); //Getting new height and width
    $img = str_replace('data:image/jpeg;base64,', '', $original); //Getting the base64 image
    $image = imagecreatefromstring(base64_decode($img));
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
    imagejpeg($new_image, $path.$name.'.jpg'); // saving the image
}

save_image_from_64("cdn/","test","800x200","data:image/jpeg;base64,/9j/4AAQS...");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接