开发者

Problem using imagecreatefromjpeg and imagejpeg

As per an assignment given to me, I am trying to see the effects of the following two function of php on a image file 1. imagecreatefromjpeg 2. imagejpeg

I upload the file using a html and then my php code looks like this:

 <?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 Exceptio开发者_如何学编程n('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();
}

    ?>

But when i do this, i get the following output:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 10368 bytes) in C:\Users\zee\Documents\Flex Builder 3\CLOUD\bin-debug\upload_file.php on line 3

The size of original image is : 5,136 KB give the above error after running the php.

But if i try for other image with size : 2,752 KB It Works ..

Can someone please help me with this. Zeeshan


First of all drop the header("Content-type:image/jpeg"); line, it's doing nothing there since you're using the filename argument of the imagejpeg() function.

Secondly, to avoid memory problems you should change the memory limit, something like:

ini_set('memory_limit', -1);

Should solve your problems (place it at the beginning of the file).

To restore the original memory limit you can add the following line at the end of the file:

ini_restore('memory_limit');

The whole script should look something like this:

<?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');

?>


You are requesting the file's name, not the path to the file. Try:

$imgname = $_FILES["file"]["tmp_name"];


Warning: Cannot modify header information - headers already sent by

Verify that are aren't any characters before the opening PHP tag; that is the reason of getting that error message. If you don't see any character, it could be the file starts with the BOM sequence, which is a character sequence that in a UTF file allows to understand if the file is encoded in UTF-8, UTF-16 LE, or UTF-16 BE.


It is because you are outputting text via echo. Headers cannot be sent again, because they were already sent in order to send your text. Consider output buffering.

See my response to a similar question.

EDIT: Also, see Steve's post about using the 'tmp_name' index in the $_FILES array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜