PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 20, 2014 7:25 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri Feb 18, 2011 4:44 pm 
Offline
Forum Newbie

Joined: Thu Feb 17, 2011 5:38 pm
Posts: 4
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:

Syntax: [ Download ] [ Hide ]
<?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


Top
 Profile  
 
PostPosted: Fri Feb 18, 2011 7:23 pm 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 4990
Location: Indiana
Have you turned error reporting on and see what it says?

Place this at the top of your script:

Syntax: [ Download ] [ Hide ]
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); ?


Top
 Profile  
 
PostPosted: Sat Feb 19, 2011 6:27 am 
Offline
Forum Newbie

Joined: Thu Feb 17, 2011 5:38 pm
Posts: 4
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!


Top
 Profile  
 
PostPosted: Sat Feb 19, 2011 6:43 am 
Offline
Forum Newbie

Joined: Thu Feb 17, 2011 5:38 pm
Posts: 4
Well, I think I'm getting it working right!!
Syntax: [ Download ] [ Hide ]
        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


Top
 Profile  
 
PostPosted: Sat Feb 19, 2011 8:35 pm 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 4990
Location: Indiana
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


Top
 Profile  
 
PostPosted: Sun Feb 20, 2011 2:19 pm 
Offline
Forum Newbie

Joined: Thu Feb 17, 2011 5:38 pm
Posts: 4
Done!
Thank you again. Everything seems to be OK now. :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group