Page 1 of 1

Some JPEG working fine, other are black with imagereatefromj

Posted: Fri Feb 18, 2011 3:44 pm
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

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

Posted: Fri Feb 18, 2011 6:23 pm
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); ?

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

Posted: Sat Feb 19, 2011 5:27 am
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!

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

Posted: Sat Feb 19, 2011 5:43 am
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

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

Posted: Sat Feb 19, 2011 7:35 pm
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

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

Posted: Sun Feb 20, 2011 1:19 pm
by SamSuffit
Done!
Thank you again. Everything seems to be OK now. :D