PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Apr 19, 2018 10:20 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Tue Jul 20, 2004 1:11 am 
Offline
Forum Newbie

Joined: Tue Jul 20, 2004 1:11 am
Posts: 4
Location: California
Can someone please help me? I am totally frustrated trying to create a thumbnail image from a image uploaded through a form. I've never posted anything before so I hope my explaination isn't too long. Here it goes...

I have some code that will upload a jpeg image through a form, scale it to a specific size, save it to my server and enter the file name into a mySQL table. This all works perfectly. The problem comes when I try to also create a thumbnail from the image that I just uploaded and save it into a /thumbs directory. Here is the function that does the resize:

Syntax: [ Download ] [ Hide ]
<?php



$imgsize = getimagesize($userfile1);

if(($imgsize[0] > 350 || $imgsize[1] > 350))

{

    $tmpimg = tempnam("/tmp","MKUP");

                                       

    // resize large image

    system("djpeg $userfile1 > $tmpimg");

    system("pnmscale -xy 350 350 $tmpimg | cjpeg -smoo 10 -qual 90 >$userfile1");

                               

    unlink($tmpimg);

}              



// process the image file uploaded by the form

$safefilename = ereg_replace("[^A-Za-z0-9._]", "",str_replace(" ", "_",strtolower($_FILES['userfile1']['name'])));

$destfilepath = $imagepath.$safefilename;

$destfilepath_thumb =  $imagepath.'thumbs/t_'.$safefilename;



// move the image file to the directory on the server

if (!move_uploaded_file($_FILES['userfile1']['tmp_name'], $destfilepath))

{

    echo "<p><strong>There was a problem uploading your image</strong>.</p>";

    exit;

}



// $destfilepath  =  "/home/rightons/public_html/client/staging/levonian/gallery/uploaded_images/body_center_image.jpg" ** these are the values that are being passed into the function, shown here for information only



// $destfilepath_thumb = "/home/rightons/public_html/client/staging/levonian/gallery/uploaded_images/thumbs/t_body_center_image.jpg"





createthumb($destfilepath,$destfilepath_thumb,60,60);





function createthumb($name,$filename,$new_w,$new_h){

        echo $name;

        $system=explode(".",$name);

        if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}

        if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}

        $old_x=imageSX($src_img);

        $old_y=imageSY($src_img);

        if ($old_x > $old_y) {

                $thumb_w=$new_w;

                $thumb_h=$old_y*($new_h/$old_x);

        }

        if ($old_x < $old_y) {

                $thumb_w=$old_x*($new_w/$old_y);

                $thumb_h=$new_h;

        }

        if ($old_x == $old_y) {

                $thumb_w=$new_w;

                $thumb_h=$new_h;

        }

        $gd2 = "yes"; // force imagecopyresampled

        if ($gd2==""){

                        $dst_img=ImageCreate($thumb_w,$thumb_h);

                        imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

        }else{

                $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

                imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);  

        }

        if (preg_match("/png/",$system[1])){

                imagepng($dst_img,$filename);

        } else {

                imagejpeg($dst_img,$filename);

        }

        imagedestroy($dst_img);

        imagedestroy($src_img);

}



?>


The file is opened and saved into the thumbs directory but it isn't resized. It keeps the same dimension as the original image. Why? I get no error messages. The correct image sizes are being passed into the imagecopyresampled function.

Please, any help would be very appreciated. It shouldn't be that difficult to make a thumbnail! Aargh!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 1:20 am 
Offline
Forum Commoner

Joined: Sat Jul 17, 2004 2:20 am
Posts: 41
Am I mistaken, or have you invoked the createthumb() function before defining it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 1:24 am 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 08, 2003 6:53 pm
Posts: 2101
Location: Sweden
Sorry, currently unable to test it locally. But just wanted to reply to lolpix; Doesn't matter here.

Syntax: [ Download ] [ Hide ]
echo something('foo');

    function something($string) {

        return 'bar';

    }

  // prints bar


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 2:04 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
Moving to Graphics


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 2:11 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
The problem could possibly by from stripping all periods out of the filename, then exploding on periods..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 9:38 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6444
Location: 53.01N x 112.48W
Is $src_img being created for certain?

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 9:44 am 
Offline
Forum Newbie

Joined: Tue Jul 20, 2004 1:11 am
Posts: 4
Location: California
when I echo the $src_img variable I get "Resource id #12" Could this be the problem? Remember, the thumbnail image gets created but it gets created without being resized, it keeps the original dimensions.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 9:50 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6444
Location: 53.01N x 112.48W
No, that's not the problem. It outputs that because $src_img is not a variable that can be output in a human readable form.

Echo $thumb_w and $thumb_h right before you call ImageCreate - your math might be wrong. Speaking of which, what's the purpose of:
Syntax: [ Download ] [ Hide ]
if ($old_x > $old_y) {

      $thumb_w=$new_w;

      $thumb_h=$old_y*($new_h/$old_x);

   }

   if ($old_x < $old_y) {

      $thumb_w=$old_x*($new_w/$old_y);

      $thumb_h=$new_h;

   }

   if ($old_x == $old_y) {

      $thumb_w=$new_w;

      $thumb_h=$new_h;

   }


If you're just trying to maintain ratios, I'm sure it can be done simpler :)

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:21 am 
Offline
Forum Newbie

Joined: Tue Jul 20, 2004 1:11 am
Posts: 4
Location: California
here is my output of $thumb_w and $thumb_h just before I call

Syntax: [ Download ] [ Hide ]
<?php



$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);



?>


40.4571428571, 60

any clues?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:31 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6444
Location: 53.01N x 112.48W
Very few to be honest, try
Syntax: [ Download ] [ Hide ]
echo imagesx($dest_img);

echo imagesy($dest_img);


Seems kind of pointless, but you never know :?

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
PostPosted: Tue Jul 20, 2004 10:35 am 
Offline
Forum Newbie

Joined: Tue Jul 20, 2004 1:11 am
Posts: 4
Location: California
It's a miracle!!!

I just tested it again and it works! I don't know what happened, I don't think that I modified anything, but it is working! Pickle, thank you so much for helping me with this.

Consider this resolved!

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 11:18 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6444
Location: 53.01N x 112.48W
Not sure what I did exactly, but you're welcome :)

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 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