减少用于图像大小调整的php内存使用量 [英] reducing php memory usage for image resizing

查看:63
本文介绍了减少用于图像大小调整的php内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个PHP网站,用户需要能够上传图像,然后将其调整为合理的尺寸.我的php配置设置为50mb的内存限制.

I'm currently creating a PHP website where users need to be able to upload images, which then need to resized down to a reasonable size. My php configuration is set to a memory limit of 50mb.

问题是当上传超过5mb的图像时,php无法调整图像大小(由于占用大量内存).因此,我想知道是否有某种方法可以优化调整大小图像的内存使用.这是我目前使用的:

The problem is when uploading images over around 5mb, php is not able to resize the image(due to to much memory use). So I was wondering if there's anyway to optimize memory usage of a resizing images. Here's what I currently use:

class Image {


  var $uploaddir;
  var $quality = 80;
  var $ext;
  var $dst_r;
  var $img_r;
  var $img_w;
  var $img_h;
  var $output;
  var $data;
  var $datathumb;

  function setFile($src = null) {
   $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
   if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
    $this->img_r = ImageCreateFromJPEG($src);
   } elseif(is_file($src) && $this->ext == "PNG") {
    $this->img_r = ImageCreateFromPNG($src);      
   } elseif(is_file($src) && $this->ext == "GIF") {
    $this->img_r = ImageCreateFromGIF($src);
   }
   $this->img_w = imagesx($this->img_r);
   $this->img_h = imagesy($this->img_r);
  }

  function resize($largestSide = 100) {
    $width = imagesx($this->img_r);
    $height = imagesy($this->img_r);
    $newWidth = 0;
    $newHeight = 0;

    if($width > $height){
      $newWidth = $largestSide;
      $newHeight = $height * ($newWidth / $width);
    }else{
      $newHeight = $largestSide;
      $newWidth = $width * ($newHeight / $height);
    }

   $this->dst_r = ImageCreateTrueColor($newWidth, $newHeight);
   imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
   $this->img_r = $this->dst_r;
   $this->img_h = $newHeight;
   $this->img_w = $newWidth;
  }

  function createFile($output_filename = null) {
   if($this->ext == "JPG" OR $this->ext == "JPEG" OR $this->ext == "PNG" OR $this->ext == "GIF") {
    imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'."jpg", $this->quality);
   } /*elseif($this->ext == "PNG") {
    imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
   } elseif($this->ext == "GIF") {
    imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
   }*/
   $this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
  }

  function setUploadDir($dirname) {
   $this->uploaddir = $dirname;
  }

  function flush() {
   $tempFile = $_FILES['Filedata']['tmp_name'];
   $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
   $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

   imagedestroy($this->dst_r);
   unlink($targetFile);
   imagedestroy($this->img_r);

  }

然后调整大小:

  $tempFile = $_FILES['Filedata']['tmp_name'];
  $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
  $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

  move_uploaded_file ($tempFile, $targetFile);

  $image = new Image();
  $image->setFile($targetFile);
  $image->setUploadDir($targetPath);
  $image->resize(200);
  $image->createFile(md5($id));
  $image->flush();

我当前使用的是uploadify,但是它使用此上载/调整大小脚本.无论如何,是否有一些代码可以优化以减少内存使用量?

I'm current using uploadify, but it uses this upload / resize script. Is there anyway to optimize this code so that it uses less memory?

谢谢:)

推荐答案

在300DPI下,一个5000 x 5000像素的图像将为16.7英寸,而在5.2 Mb处将出现此图像.您可能想对图像尺寸设置一些合理的期望值,例如约10兆像素?

a 5000 x 5000 pixel image would be 16.7 inches at 300DPI and that would come in at 5.2 Mb. you may want to set some reasonal expectation on image sizes, say about 10 megapixels?

一张14兆像素的图像尺寸为4672 x 3104,而JPEG格式恰好在5 Mb的图像尺寸限制内.

a 14 megapixel image measures 4672 x 3104 and as a JPEG would fit well within a 5 Mb image size restriction.

还查看并查看通过使用诸如imagecreate而不是imagecreatetruecolor的托盘化方法是否有足够的内存节省.

also look and see if there is enough memory savings by using a palleted method such as imagecreate as opposed to imagecreatetruecolor.

php站点上已经有一些帮助,可以根据图像大小确定内存需求.此处

there is also some help already on the php site for determining your memory requirements based on image size here

这篇关于减少用于图像大小调整的php内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆