PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Aug 09, 2020 6:53 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Oct 07, 2005 12:55 pm 
Offline
Forum Newbie

Joined: Fri Oct 07, 2005 12:45 pm
Posts: 1
Location: Antigua, Guatemala
feyd | Please use
Syntax: [ Download ] [ Hide ]
and
Syntax: [ Download ] [ Hide ]
tags where appropriate when posting code. Read:  </span>





I am having trouble with the following bit of code. The intention is to take an image file that will be uploaded and resize it until it is of an acceptable filesize. I have the function resize($imageFile) that does the resizing which is working. I then have a function adjustFileSize($imageFile) which I want to call the resize function until the filesize of the image is of an acceptable size. The adjustFileSize bit is not working properly. The problem seems to be that after it calls the resize function, which is resizing the image,  the $filesize variable does not change to reflect the smaller filesize of the image. Any help would be much appreciated. Here is the code:
Syntax: [ Download ] [ Hide ]
function resize($imageFile)

        {

        // File and new size

        $filename = $imageFile;

        $percent = 0.66;

       



        // Get new sizes

        list($width, $height) = getimagesize($filename);

        $newwidth = $width * $percent;

        $newheight = $height * $percent;

       

        // Load

        $thumb = imagecreatetruecolor($newwidth, $newheight);

         

        preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageFile, $ext);

        switch (strtolower($ext[2]))

                {

                   case 'jpg' :

                   case 'jpeg': $source = imagecreatefromjpeg($filename);

                                                 break;

                   case 'gif' : $source = imagecreatefromgif($filename);

                                                 break;

                   case 'png' : $source = imagecreatefrompng($filename);

                                                 break;

           }





        // Resize

        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

       

        // Output

       

        preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageFile, $ext);

        switch (strtolower($ext[2]))

                {

                   case 'jpg' :

                   case 'jpeg':imagejpeg($thumb,$filename,50);

                                                 break;

                   case 'gif' :imagegif($thumb,$filename);

                                                 break;

                   case 'png' : imagepng($thumb,$filename);

                                                 break;

           }

        }



//resize image until filesize is apropriate    

function adjustFileSize($imageFile, $maxFileSize)      

        {

        $filesize = filesize($imageFile);

        do

                {

                resize($imageFile);

                $filesize = filesize($imageFile);

                }

        while($filesize > $maxFileSize);

        }



feyd | Please use
Syntax: [ Download ] [ Hide ]
and
Syntax: [ Download ] [ Hide ]
tags where appropriate when posting code. Read:  </span>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 1:17 pm 
Offline
Neighborhood Spidermoddy
User avatar

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 2:24 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
echo $filesize in your do...while loop and see what you get. Are you sure that resize() is resizing the image properly?

_________________
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  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 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