imagecreatetruecolor php占用了太多内存

时间:2016-03-17 21:55:10

标签: php

我有一个PHP代码,通过ajax调用获取其值。这些值由用户用于裁剪图像的裁剪模块提供。

<?php

ini_set('Memory_limit', -1);

$imgUrl = $_POST['imgUrl'];
// original sizes
$imgInitW = $_POST['imgInitW'];
$imgInitH = $_POST['imgInitH'];
// resized sizes

$imgW = $_POST['imgW'];
$imgH = $_POST['imgH'];
// offsets
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];
// crop box
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];
// rotation angle
$angle = $_POST['rotation'];

$jpeg_quality = 100;
$uniquename=uniqid().time();
$output_filename = "croppedimages/".$uniquename;
// uncomment line below to save the cropped image in the same location as the original image.
//$output_filename = dirname($imgUrl). "/croppedImg_".rand();

$what = getimagesize($imgUrl);

switch(strtolower($what['mime']))
{
    case 'image/png':
        $img_r = imagecreatefrompng($imgUrl);
        $source_image = imagecreatefrompng($imgUrl);
        $type = '.png';
        break;
    case 'image/jpeg':
        $img_r = imagecreatefromjpeg($imgUrl);
        $source_image = imagecreatefromjpeg($imgUrl);
        error_log("jpg");
        $type = '.jpeg';
        break;
    case 'image/gif':
        $img_r = imagecreatefromgif($imgUrl);
        $source_image = imagecreatefromgif($imgUrl);
        $type = '.gif';
        break;
    default: die('image type not supported');
}


//Check write Access to Directory

if(!is_writable(dirname($output_filename))){
    $response = Array(
        "status" => 'error',
        "message" => 'Can`t write cropped File'
    );  
}else{

    // resize the original image to size of editor
    $resizedImage = imagecreatetruecolor($imgW, $imgH);
    imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
    // rotate the rezized image
    $rotated_image = imagerotate($resizedImage, -$angle, 0);
    // find new width & height of rotated image
    $rotated_width = imagesx($rotated_image);
    $rotated_height = imagesy($rotated_image);
    // diff between rotated & original sizes
    $dx = $rotated_width - $imgW;
    $dy = $rotated_height - $imgH;
    // crop rotated image to fit into original rezized rectangle
    $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
    imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
    imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
    // crop image into selected area
    $final_image = imagecreatetruecolor($cropW, $cropH);
    imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
    imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
    // finally output png image
    //imagepng($final_image, $output_filename.$type, $png_quality);
    imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
    $response = Array(
        "status" => 'success',
        "url" => $output_filename.$type,
    );

}
print json_encode($response);

此处 cropW cropH 是裁剪图像的值。但是,这适用于低分辨率图像,但是当使用高分辨率图像时,控制台会显示错误在imagecreatetruecolor函数。

  

致命错误:允许的内存大小为134217728 Bytes Exhausted

现在,即使我按ini_set('Memory_limit', -1);设置内存使用量,它仍然会向我显示此错误。可能是什么原因以及解决方案是什么?

注意:我知道设置ini_set('Memory_limit', -1);是一个坏主意。

1 个答案:

答案 0 :(得分:1)

如果您检查了php.ini文件,则会memory_limit Memory_limit而不是ini_set('Memory_limit','-1')

见这里: - http://prntscr.com/agxrtt

所以将ini_set('memory_limit',256)更改为ini_set('memory_limit','-1')from setuptools import setup setup(name='foo', version='0.0.1', description='Lol', url='https://github.com/foo/foo', author='legend', author_email='lol@gmail.com', license='GPLv3', packages=['foo'], install_requires=["bar"], entry_points = {'console_scripts': ['foo = foo:main']}, keywords = ['foo'], zip_safe=False)