使用imagecreatefromjpeg和imagejpeg时出现问题

Zee*_*ang 1 php

根据赋予我的任务,我试图看到php的以下两个函数对图像文件的影响1. imagecreatefromjpeg 2. imagejpeg

我使用html上传文件,然后我的PHP代码如下所示:

 <?php

 try{
   if(!$image=imagecreatefromjpeg('zee1.jpg')){
      throw new Exception('Error loading image');
   }
   // create text color for jpg image
   if(!$textColor=imagecolorallocate($image,0,255,0)){
      throw new Exception('Error creating text color');
   }
   // include text string into jpg image
   if(!$text=imagestring($image,5,10,90,'This is a sample text
string.',$textColor)){
      throw new Exception('Error creating image text');
   }
   header("Content-type:image/jpeg");
   // display image
   imagejpeg($image, 'zee1After.jpg');
   // free up memory
   imagedestroy($image);
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

    ?>
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我得到以下输出:

致命错误:第3行的C:\ Users\zee\Documents\Flex Builder 3\CLOUD\bin-debug\upload_file.php中允许的内存大小为33554432字节(尝试分配10368字节)

原始图像的大小是:5,136 KB运行php后出现上述错误.

但如果我尝试其他大小的图像:2,752 KB它的工作..

有人可以帮我这个.Zeeshan

Ali*_*xel 5

首先放弃该header("Content-type:image/jpeg");行,因为你正在使用imagejpeg()函数的filename参数,所以它什么也没做.

其次,为避免内存问题,您应该更改内存限制,例如:

ini_set('memory_limit', -1);
Run Code Online (Sandbox Code Playgroud)

应该解决你的问题(把它放在文件的开头).

要恢复原始内存限制,可以在文件末尾添加以下行:

ini_restore('memory_limit');
Run Code Online (Sandbox Code Playgroud)

整个脚本看起来应该是这样的:

<?php

ini_set('memory_limit', -1);

try 
{
    if (!$image = imagecreatefromjpeg('zee1.jpg'))
    {
        throw new Exception('Error loading image');
    }

    // create text color for jpg image
    if (!$textColor = imagecolorallocate($image, 0, 255, 0))
    {
        throw new Exception('Error creating text color');
    }

    // include text string into jpg image
    if (!$text = imagestring($image, 5, 10, 90, 'This is a sample text string.', $textColor))
    {
        throw new Exception('Error creating image text');
    }

    // display image
    imagejpeg($image, 'zee1After.jpg');

    // free up memory
    imagedestroy($image);
}

catch (Exception $e)
{
    echo $e->getMessage();
    exit();
}

ini_restore('memory_limit');

?>
Run Code Online (Sandbox Code Playgroud)