【问题标题】:Problem using imagecreatefromjpeg and imagejpeg使用 imagecreatefromjpeg 和 imagejpeg 的问题
【发布时间】:2009-12-23 03:52:31
【问题描述】:

根据给我的任务,我正在尝试查看以下两个 php 函数对图像文件的影响 1.imagecreatefromjpeg 2.图片jpeg

我使用 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();
}

    ?>

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

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

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

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

有人可以帮我解决这个问题吗? 泽山

【问题讨论】:

    标签: php


    【解决方案1】:

    首先删除 header("Content-type:image/jpeg"); 行,因为您使用的是 imagejpeg() 函数的文件名参数,所以它在那里什么都不做。

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

    ini_set('memory_limit', -1);
    

    应该可以解决您的问题(将其放在文件的开头)。

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

    ini_restore('memory_limit');
    

    整个脚本应该是这样的:

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

    【讨论】:

      【解决方案2】:

      您请求的是文件名,而不是文件的路径。试试:

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

      【讨论】:

        【解决方案3】:

        警告:无法修改标题 信息 - 已发送的标头

        验证开始 PHP 标记之前不是任何字符;这就是收到该错误消息的原因。如果您没有看到任何字符,则可能是文件以 BOM 序列开头,这是一个字符序列,在 UTF 文件中可以理解文件是用 UTF-8、UTF-16 LE 还是 UTF 编码的-16 BE。

        【讨论】:

          【解决方案4】:

          这是因为您正在通过 echo 输出文本。无法再次发送标头,因为它们已经被发送以发送您的文本。考虑输出缓冲。

          See my response to a similar question.

          编辑:另外,请参阅 Steve 关于在 $_FILES 数组中使用“tmp_name”索引的帖子。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-14
            • 2012-08-27
            • 2018-04-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多