Some JPEG working fine, other are black with imagereatefromj

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
SamSuffit
Forum Newbie
Posts: 4
Joined: Thu Feb 17, 2011 4:38 pm

Some JPEG working fine, other are black with imagereatefromj

Post by SamSuffit »

Hi,
Sorry if this topic has already been treated. It's my first time here. Five days that I am searching for a solution but can't find it.

My problem : I am trying to create a Facebook application that takes your profile picture and inserts it into a PNG picture.
Everything is working fine with some profile pictures and with others, I'm getting a black screen where profile picture is supposed to be inserted.
But variables are OK. File paths are OK.
I tried with many different profile pictures. With original photos, it's OK. With some cropped pics, its OK also. And some other won't work. Square cropped pics won't work.

Can someone help me out with that ? ... I'm not sure to understand everything I'm doing... :?

Many thanks!!!




Here's my image code:

Code: Select all

<?php

//this is the core function.
function create_pic($id,$text,$url){

$img = $_POST["image"];
$image = "visuels/".$img.".png"; 

	// creation of a new image, the final one
	$im = imagecreatetruecolor(500,500);
	
	// importing profile picture
	$profile = imagecreatefromjpeg($url);

	// importing transparent mirror image
	$mirror = imagecreatefrompng($image);

         //end function getJPEGresolution

	// retrieving profile picture size
	$sizes = getimagesize($url);
 
	$x_dim = $sizes[0];
	$y_dim = $sizes[1];
 
	// mirror transparency is 265x335, so I resize the profile picture
	// to fit this size, keeping widht/height ratio
	if($x_dim<150 or $y_dim<170){
		$mult = max(150/$x_dim,170/$y_dim);
		$final_x = $x_dim*$mult;
		$final_y = $y_dim*$mult;
	}
 
	// pasting the resized profile picture on the final image
	imagecopyresampled($im,$profile,250-$final_x/2,240-$final_y/2,0,0,$final_x,$final_y,$x_dim,$y_dim);
	// pasting the mirror on the final image
	imagecopyresampled($im,$mirror,0,0,0,0,500,500,500,500);
 
	// creating some colors
	$color = imagecolorallocate($im, 207, 0, 75);
	$shadow = imagecolorallocate($im, 0, 0, 0);
 
	// this is the path of the font I am using
	$font = "visuels/font.ttf";
 
	// starting at font size 0
	$size = 0;
	// boolean variable to determine if the font fits on the picture
	$it_fits = true;
	// increasing font zize by one unit until it won't fit anymore
	do{
	     $last_dim = $dim;
	     $size++;
	     $dim = imageftbbox($size, 0, $font, $text);
	     if($dim[4]-$dim[6]>300){
	          $it_fits=false;
	     }
	} while($it_fits);
 
	$center = floor((500-$last_dim[4]+$last_dim[6])/2);
	
	imagettftext($im, $size-1, 0, $center, 100,  $shadow, $font, $text);
	imagettftext($im, $size-1, 0, $center-1, 100-1,  $color, $font, $text);

	imagejpeg($im,"temp/".$id.".jpg",100);

	header('Content-type:image/jpeg');	
	imagedestroy($im);
	imagedestroy($profile);
	imagedestroy($mirror);
}
?>

<?php

// core function: creation of the horror picture
create_pic($uid,$text,$user_image);


My GD Library :

GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.2.1
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XPM Support enabled
XBM Support enabled





scottayy | added syntax tags
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Some JPEG working fine, other are black with imagereatef

Post by s.dot »

Have you turned error reporting on and see what it says?

Place this at the top of your script:

Code: Select all

ini_set('display_errors', 'On');
error_reporting(E_ALL);
From a quick glance at your script, it appears that if $x_dim < 150 or $y_dim < 170, that $final_x and $final_y are never set. This could cause problems.
Also, make sure you're receiving an image in the function. Is $_POST['image'] actually an image? does getimagesize() fail on getimagesize($url); ?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
SamSuffit
Forum Newbie
Posts: 4
Joined: Thu Feb 17, 2011 4:38 pm

Re: Some JPEG working fine, other are black with imagereatef

Post by SamSuffit »

Thank you already for your help. You're right about the problem!

Here's the error report:

X : 200 Y : 200 Mult :

Notice: Undefined variable: mult in /home/jaunever/public_html/appli_facebook/pages/image.php on line 46

Notice: Undefined variable: final_x in /home/jaunever/public_html/appli_facebook/pages/image.php on line 48

Notice: Undefined variable: final_y in /home/jaunever/public_html/appli_facebook/pages/image.php on line 48

Notice: Undefined variable: final_x in /home/jaunever/public_html/appli_facebook/pages/image.php on line 48

Notice: Undefined variable: final_y in /home/jaunever/public_html/appli_facebook/pages/image.php on line 48

Notice: Undefined variable: dim in /home/jaunever/public_html/appli_facebook/pages/image.php on line 65

But my $_POST['image'] is returning my image and getimagesize($url); is also returning a size. That's OK.

Now I already know where I have to work to get it fixed. But if you have any advice, it would be more than welcome! Anyway, thank you alreday very much!
SamSuffit
Forum Newbie
Posts: 4
Joined: Thu Feb 17, 2011 4:38 pm

Re: Some JPEG working fine, other are black with imagereatef

Post by SamSuffit »

Well, I think I'm getting it working right!!

Code: Select all

	if($x_dim<150 or $y_dim<170){
		$mult = max(150/$x_dim,170/$y_dim);
		$final_x = $x_dim*$mult;
		$final_y = $y_dim*$mult;
	} else {
		
		$final_x = $x_dim;
		$final_y = $y_dim;
		
		}
Will test it.

THANK YO SOOOOO MUCH !!! :D
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Some JPEG working fine, other are black with imagereatef

Post by s.dot »

Great :)
Put $mult inside of the else too.
I'm not sure why you're getting the undefined $dim error, as I'm not familiar with the imageftbbox() function but here is the manual page for it
http://us3.php.net/imageftbbox
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
SamSuffit
Forum Newbie
Posts: 4
Joined: Thu Feb 17, 2011 4:38 pm

Re: Some JPEG working fine, other are black with imagereatef

Post by SamSuffit »

Done!
Thank you again. Everything seems to be OK now. :D
Post Reply