【问题标题】:How to resize image through php script如何通过php脚本调整图像大小
【发布时间】:2013-07-22 01:05:48
【问题描述】:

我正在创建一个应用程序,该应用程序需要通过 php 脚本将大图像转换为缩略图,然后将其编码为 base64,以便我可以通过 json 将其发送到我的 android 应用程序。 我在调整图像大小时遇到​​问题。我需要 php 脚本来帮助我做到这一点

【问题讨论】:

  • 你使用的图像处理库是什么?

标签: php image base64


【解决方案1】:

你可以试试这个Image Resize功能tutorial

您也可以将此代码用于调整大小功能(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】:

以下代码创建一个名为 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);
?>

【讨论】:

    【解决方案3】:

    您可以尝试 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(); 
    
    ?>
    

    【讨论】:

      【解决方案4】:

      如果您使用的是 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);
      ?>
      

      【讨论】:

        【解决方案5】:

        您可以使用例如TimThumb

        timthumb.php?src=img.jpg&h=height&w=width
        

        然后您应该将图像编码为 base64: How to convert an image to base64 encoding?

        【讨论】:

          【解决方案6】:

          帖子有点老了,但如果有人想要,我只是做了这个功能

          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...");
          

          【讨论】:

            【解决方案7】:

            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();
            

            【讨论】:

              猜你喜欢
              • 2018-05-22
              • 1970-01-01
              • 2012-02-14
              • 1970-01-01
              • 2018-06-03
              • 2013-07-04
              • 2012-12-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多