PHP Developers Network
http://forums.devnetwork.net/

[SOLVED] Why won't "imagecopyresampled" resize the
http://forums.devnetwork.net/viewtopic.php?f=32&t=24077
Page 1 of 1

Author:  RightOnLM [ Tue Jul 20, 2004 1:11 am ]
Post subject:  [SOLVED] Why won't "imagecopyresampled" resize the

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!!

Author:  lolpix [ Tue Jul 20, 2004 1:20 am ]
Post subject: 


Author:  JAM [ Tue Jul 20, 2004 1:24 am ]
Post subject: 

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

Author:  feyd [ Tue Jul 20, 2004 2:04 am ]
Post subject: 

Moving to Graphics

Author:  feyd [ Tue Jul 20, 2004 2:11 am ]
Post subject: 

The problem could possibly by from stripping all periods out of the filename, then exploding on periods..

Author:  pickle [ Tue Jul 20, 2004 9:38 am ]
Post subject: 

Is $src_img being created for certain?

Author:  RightOnLM [ Tue Jul 20, 2004 9:44 am ]
Post subject: 

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.

Author:  pickle [ Tue Jul 20, 2004 9:50 am ]
Post subject: 

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 :)

Author:  RightOnLM [ Tue Jul 20, 2004 10:21 am ]
Post subject: 

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?

Author:  pickle [ Tue Jul 20, 2004 10:31 am ]
Post subject: 

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 :?

Author:  RightOnLM [ Tue Jul 20, 2004 10:35 am ]
Post subject:  Why won't "imagecopyresampled" resize the image? H

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!

Author:  pickle [ Tue Jul 20, 2004 11:18 am ]
Post subject: 

Not sure what I did exactly, but you're welcome :)

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/