Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用imagecreatefromjpeg和imagejpeg时出现问题_Php - Fatal编程技术网

Php 使用imagecreatefromjpeg和imagejpeg时出现问题

Php 使用imagecreatefromjpeg和imagejpeg时出现问题,php,Php,根据分配给我的任务,我正在尝试查看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 imag

根据分配给我的任务,我正在尝试查看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();
}

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

?>

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

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

原始图像的大小为:5136 KB运行php后给出上述错误

但是,如果我尝试其他大小为2752kb的图像,它会工作

谁能帮我一下吗。
Zeeshan

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

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

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

$imgname = $_FILES["file"]["tmp_name"];
警告:无法修改标题 信息-已由发送的标头

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

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


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

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


编辑:另外,请参阅Steve关于在
$\u文件
数组中使用“
tmp\u名称
”索引的帖子。

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


编辑:另外,请参阅Steve关于在
$\u文件
数组中使用“
tmp\u名称
”索引的帖子。

首先,删除
标题(“内容类型:image/jpeg”)行,因为您使用的是
imagejpeg()
函数的filename参数,所以它在那里什么也不做

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

ini_set('memory_limit', -1);
应该解决您的问题(将其放在文件的开头)

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

ini_restore('memory_limit');
整个脚本应该如下所示:

 <?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();
}

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

?>

首先删除
标题(“内容类型:image/jpeg”)行,因为您使用的是
imagejpeg()
函数的filename参数,所以它在那里什么也不做

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

ini_set('memory_limit', -1);
应该解决您的问题(将其放在文件的开头)

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

ini_restore('memory_limit');
整个脚本应该如下所示:

 <?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();
}

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

?>