php上传文件是 内存减少,减少用于图像大小调整的php内存使用量

我目前正在创建一个PHP网站,用户需要能够上传图像,然后需要调整大小到合理的大小。我的php配置设置为50mb的内存限制。

问题是当上传大约5mb的图像时,php无法调整图像大小(由于大量内存使用)。所以我想知道是否还有优化调整大小图像的内存使用量。这是我目前使用的:

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,但它使用此上传/调整大小脚本。是否有优化此代码,以便它使用更少的内存?

谢谢 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值