php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54482 imagecreatefromstring fails
Submitted: 2011-04-07 06:53 UTC Modified: 2011-04-11 23:27 UTC
From: info822 at freshsoftware dot net Assigned: pajoye (profile)
Status: Not a bug Package: GD related
PHP Version: 5.3.6 OS: Fedora 13 (Godard) Kernel Linux
Private report: No CVE-ID: None
 [2011-04-07 06:53 UTC] info822 at freshsoftware dot net
Description:
------------
I am writing you because "imagecreatefromstring" is causing images to output black images. I have been using the same script on a live server running CentOS/PHP-5.1.6 and a development server running Fedora 9/PHP-5.2.3 for 1+ year with no problem.

Recently I upgraded the dev server to Fedora 13/PHP-5.3.6 and now the same script has stopped displaying images. I have recompiled PHP, reinstalled http, httpd-devel, gd, gd-devel, libjpeg, libjpeg-devel, libpng, libpng-devel, and other related packages numerous times. But I can not figure out the problem. I would install PHP-5.2.3 on Fedora 13 but Openssl error out during the make process.


******************************************************************************

Here is some information about the liver server that does not have a problem.

phpinfo: https://secure20.nexcess.net/thewordsmith.info/php/

Script View: https://secure20.nexcess.net/thewordsmith.info/resize_event_pic.php?event_num=27&new_width=100&new_height=143

Actual Image: https://secure20.nexcess.net/thewordsmith.info/images/event_images/2010-11-20/The%20Lake%20Effect%20Festival/Lake%20Effect%20Hip%20Hop%20Flyer.jpg

Product script View: https://secure20.nexcess.net/thewordsmith.info/calendar.php

******************************************************************************

Here is info about the development server with the "imagecreatefromstring" problem:

phpinfo: http://thewordsmith.dyndns.info/php/

Script View: http://thewordsmith.dyndns.info/resize_event_pic.php?event_num=27&new_width=100&new_height=143

Actual image: http://thewordsmith.dyndns.info/images/event_images/2010-11-20/The%20Lake%20Effect%20Festival/Lake%20Effect%20Hip%20Hop%20Flyer.jpg

Production Script View: http://thewordsmith.dyndns.info/calendar.php 


******************************************************************************

The logs are putting out "chunk" and "Corrupt JPEG data: 18580 extraneous bytes before marker 0xe2" errors. You can view all 3 error logs at:

http://thewordsmith.dyndns.info/error_logs/

The development server is only on from 10am-6pm EST time. Email me when you are ready to look at this issue so I can be sure that the dev server is on.


Test script:
---------------
<?
//ob_start();

//echo $HTTP_SERVER_VARS['HTTP_REFERER'];
//DB connection.
require "../db.php";
require "../globals.php"
/*
header ("Expires: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header ("Cache-Control: no-cache, no-store, must-revalidate, private, max-age=0, proxy-revalidate, no-transform");
header ("Pragma: no-cache");
header("Vary: User-Agents");
*/

//Now select the meber info.
$get_calendar_info = mysql_query("SELECT * FROM `calandar` WHERE `event_num` = '$event_num' && `event_active` = 'yes'")
or die(mysql_error(). ": " . __FILE__ . ": " . __LINE__);
//or die(header("location: http://$none_secured_domain/error_page.php?profile_name_not_found"));
$calendar_info = mysql_fetch_object($get_calendar_info);

//Die to an errror if no such profile name exist.
if(mysql_num_rows($get_calendar_info) <= 0){
	mysql_close($mysql_connect);
	//die(header("location: http://$none_secured_domain/error_page.php?profile_name_not_found"));
}

if($calendar_info->event_img != ""){
	$calendar_info->event_img = "$img_dir" . "/" . "$calendar_info->event_img";
}
else{
	//$calendar_info->event_img = "$img_dir" . "/" . "site/Calendar-Logo-256x256.png";
}

$img_info  = getimagesize("$calendar_info->event_img");

// get contents of a file into a string
$img_data = file_get_contents("$calendar_info->event_img");

$thumb = imagecreatetruecolor($new_width, $new_height);// or die("Could create true color image. Error on file: " . __FILE__ . " Line: " . __LINE__);
$source = imagecreatefromstring($img_data);// or die("Could create image from string. Error on file: " . __FILE__ . " Line: " . __LINE__);

// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $img_info[0], $img_info[1]);// or die("Could not copy image Error on file: " . __FILE__ . " Line: " . __LINE__);

// Output
if($img_info[2] == 1){
	header("Content-Length: " . filesize("$calendar_info->event_img"));
	header("Content-type: " . image_type_to_mime_type(IMAGETYPE_GIF));
	imagegif($thumb);
}
elseif($img_info[2] == 2){
	header("Content-Length: " . filesize("$calendar_info->event_img"));
	header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG));
	imagejpeg($thumb);
}
elseif($img_info[2] == 3){
	header("Content-Length: " . filesize("$calendar_info->event_img"));
	header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
	imagepng($thumb);
}
imagedestroy($thumb);
//ob_flush();
?> 

Expected result:
----------------
Output of a thumbesize image which is reduced from a larger image.

Actual result:
--------------
A black images is out puted instead of a thumbebsize copy of a larger image.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-07 17:52 UTC] info822 at freshsoftware dot net
A list off all the rpms installed on my system can be viewed at: http://thewordsmith.dyndns.info/rpms_installed.txt
 [2011-04-07 21:59 UTC] pajoye@php.net
-Status: Open +Status: Feedback
 [2011-04-07 21:59 UTC] pajoye@php.net
Cannot reproduce.

Here is a simple script:
<?php
$file = 
'https://secure20.nexcess.net/thewordsmith.info/images/event_images/2010-11-
20/The%20Lake%20Effect%20Festival/Lake%20Effect%20Hip%20Hop%20Flyer.jpg';

imagepng(imagecreatefromjpeg($file), 'a1.png');
imagepng(imagecreatefromstring(file_get_contents($file)), 'a2.png');

a1 and a2 are just fine. Please try this script as well.
 [2011-04-07 22:48 UTC] info822 at freshsoftware dot net
-Status: Feedback +Status: Open
 [2011-04-07 22:48 UTC] info822 at freshsoftware dot net
You couldnot reporduce the error because you apparently only looked at the live server. Please look at the DEV server.

http://thewordsmith.dyndns.info/resize_event_pic.php?event_num=27&new_width=100&new_height=143
 [2011-04-07 22:52 UTC] info822 at freshsoftware dot net
file_get_contents is apparently not reading the entire file.
 [2011-04-07 23:29 UTC] pajoye@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: pajoye
 [2011-04-07 23:29 UTC] pajoye@php.net
Well, as I said, the code works. If for some reason it does not work on another 
server, then you have to verify its configuration.

Have you tried my example on the dev server? Or any other where you met this bug?
 [2011-04-07 23:57 UTC] info822 at freshsoftware dot net
-Status: Feedback +Status: Assigned
 [2011-04-07 23:57 UTC] info822 at freshsoftware dot net
Unfortunately your code does not work. Look at test1 & test2.php at:
http://thewordsmith.dyndns.info/test/

Notice that a2.png is not created in:
http://thewordsmith.dyndns.info/link_to_tmp/

Test 2 is a short version of my own code.

I will email you the SSH info and you can go at it. The PHPINFO for my dev box is available at:

http://thewordsmith.dyndns.info/php/
 [2011-04-08 00:34 UTC] pajoye@php.net
-Status: Assigned +Status: Feedback
 [2011-04-08 00:34 UTC] pajoye@php.net
Sorry but I can't do it now.

Which version of libjpeg and PHP do you use on the servers where this code fails?
 [2011-04-08 00:50 UTC] info822 at freshsoftware dot net
-Status: Feedback +Status: Assigned
 [2011-04-08 00:50 UTC] info822 at freshsoftware dot net
I am using libjpeg-6b-46fc.i686, libpng-1.2.44-1.fc13.i686, gd-2.0.35-11.fc13.i686, and PHP-5.3.6. Hmmm,jpg is a beta verision. May be that is my problem. I will attempt to rollback the libjpeg package andlet you know what happens.
 [2011-04-08 02:15 UTC] info822 at freshsoftware dot net
libjpeg-6b-46.fc12.i686.rpm is the only available package. That sucks.
 [2011-04-08 11:02 UTC] pajoye@php.net
-Status: Assigned +Status: Feedback
 [2011-04-08 11:02 UTC] pajoye@php.net
Which php version is used on these servers?
 [2011-04-08 15:47 UTC] info822 at freshsoftware dot net
-Status: Feedback +Status: Assigned
 [2011-04-08 15:47 UTC] info822 at freshsoftware dot net
On the DEV Server php-5.3.6 is used. See http://thewordsmith.dyndns.info/php/

On the Live Server php-5.1.6 is used. See https://secure20.nexcess.net/thewordsmith.info/php/

Yesterday I reinstalled gd, gd-devel, libjpeg* and libpng* with no success. Also talked to several peopple via irc chat. This is a problem with libraries, PHP build, etc. I do not know how to fix this problem. You are welcome to SSH login and try to fix this problem.
 [2011-04-08 17:08 UTC] pajoye@php.net
-Status: Assigned +Status: Bogus
 [2011-04-08 17:08 UTC] pajoye@php.net
I won't do it sorry. The php code has no bug and works just fine. If you need 
support to fix your server, you may try to ask your ISP admins instead.

No bug in php > bogus.
 [2011-04-08 17:49 UTC] info822 at freshsoftware dot net
I know that the coding works. The problem is either with PHP or the libraries. How do you know that this is not a PHP problem? Just because the code is know to work does not mean that the PHP build is not experiencing problems. I am the ISP of the dev box.
 [2011-04-08 18:05 UTC] info822 at freshsoftware dot net
The code is outputing errors. Look at:

http://thewordsmith.dyndns.info/test/

Notice that a2.png is not created in:
http://thewordsmith.dyndns.info/link_to_tmp/
 [2011-04-08 18:19 UTC] pajoye@php.net
It is not PHP, it works just fine on any machine I tried.
 [2011-04-08 19:21 UTC] info822 at freshsoftware dot net
I am not the only person with that problem. Read:

http://www.zabbix.com/forum/showthread.php?t=11195

http://bugs.php.net/bug.php?id=29482

http://drupal.org/node/307028
 [2011-04-08 19:46 UTC] pajoye@php.net
Totally unrelated issues.
 [2011-04-11 23:25 UTC] info822 at freshsoftware dot net
$img_info  = getimagesize("$calendar_info->event_img");

//Create a smaller image pallet
$thumb = imagecreatetruecolor($new_width, $new_height);// or die("Could create true color image. Error on file: " . __FILE__ . " Line: " . __LINE__);
	
// Output
if($img_info[2] == 1){
	$source = imagecreatefromgif("$calendar_info->event_img");// or die("Could create image from string. Error on file: " . __FILE__ . " Line: " . __LINE__);
	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $img_info[0], $img_info[1]);// or die("Could not copy image Error on file: " . __FILE__ . " Line: " . __LINE__);

	header("Content-Length: " . filesize("$calendar_info->event_img"));
	header("Content-type: " . image_type_to_mime_type(IMAGETYPE_GIF));
	imagegif($thumb);
}
elseif($img_info[2] == 2){
	$source = imagecreatefromjpeg("$calendar_info->event_img");// or die("Could create image from string. Error on file: " . __FILE__ . " Line: " . __LINE__);
	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $img_info[0], $img_info[1]);// or die("Could not copy image Error on file: " . __FILE__ . " Line: " . __LINE__);

	header("Content-Length: " . filesize("$calendar_info->event_img"));
	header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG));
	imagejpeg($thumb);
}
elseif($img_info[2] == 3){
	$source = imagecreatefrompng("$calendar_info->event_img");// or die("Could create image from string. Error on file: " . __FILE__ . " Line: " . __LINE__);
	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $img_info[0], $img_info[1]);// or die("Could not copy image Error on file: " . __FILE__ . " Line: " . __LINE__);

	header("Content-Length: " . filesize("$calendar_info->event_img"));
	header("Content-type: " . image_type_to_mime_type(IMAGETYPE_PNG));
	imagepng($thumb);
}
imagedestroy($thumb);
?>
 [2011-04-11 23:27 UTC] info822 at freshsoftware dot net
I am closing this issue. Imagecreatefromstring does not handle large images very well. I had to rewrite my code as shown above.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 20:01:32 2024 UTC