PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Jul 22, 2019 8:12 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Jan 28, 2008 8:09 pm 
Offline
The Ninja Space Mod
User avatar

Joined: Fri Aug 05, 2005 1:53 pm
Posts: 6424
Location: Paradise, CA


Top
 Profile  
 
PostPosted: Tue Jan 29, 2008 7:25 am 
Offline
Forum Contributor
User avatar

Joined: Mon Nov 19, 2007 11:15 am
Posts: 282
Location: UK


Top
 Profile  
 
PostPosted: Tue Jan 29, 2008 11:00 am 
Offline
The Ninja Space Mod
User avatar

Joined: Fri Aug 05, 2005 1:53 pm
Posts: 6424
Location: Paradise, CA
What I'm trying to do is crop the image. I can't figure out how to do that.


Top
 Profile  
 
PostPosted: Tue Jan 29, 2008 11:22 am 
Offline
Forum Contributor
User avatar

Joined: Mon Nov 19, 2007 11:15 am
Posts: 282
Location: UK
This is a tidied up version of a couple of functions I use. First function just makes the image smaller, the second one turns it into a sqaure:

You should be able to modify one of these to do what you need. Both take a max size, which for the first function sets the max width or height, depending on which is biggest. On the second, I work out the smallest dimension, then resize the image, cropping off the excess from the longest dimension.

Syntax: [ Download ] [ Hide ]
   function resize_normal($image_type,$file_old,$file_new,$max_size)
    {
        //
        // $image_type should be either:
        //
        // png, jpg or gif
        //
        list($width, $height) = getimagesize($file_old);
        if($width <= $max_size && $height <= $max_size){
            copy($file_old, $file_new);
        } else {
            if ($width > $height){
                $resize_percent = $max_size / $width;
                $new_height = $height * $resize_percent;
                $new_width = $max_size;
            } else {
                $resize_percent = $max_size / $height;
                $new_height = $max_size;
                $new_width = $width * $resize_percent;
            }
            $temp_image = imagecreatetruecolor($new_width, $new_height);
            switch($image_type){
                case 'png':
                    $image = imagecreatefrompng($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagepng($temp_image, $file_new);
                    break;
                case 'jpg':
                    $image = imagecreatefromjpeg($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagejpeg($temp_image, $file_new);
                    break;
                case 'gif':
                    $image = imagecreatefromgif($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagegif($temp_image, $file_new);
                    break;
            }
        }
    }
 
 
    function resize_square($image_type,$file_old,$file_new,$max_size)
    {
        //
        // $image_type should be either:
        //
        // png, jpg or gif
        //
        list($width, $height) = getimagesize($file_old);
        if($width <= $max_size && $height <= $max_size){
            copy($file_old, $file_new);
        } else {
            if ($width > $height) {
                $src_w = $src_h = $height;
                $src_y = 0;
                $src_x = round(($width - $height) / 2);
            } else {
                $src_w = $src_h = $width;
                $src_x = 0;
                $src_y = round(($height - $width) / 2);
            }
            $temp_image = imagecreatetruecolor($max_size, $max_size);
            switch($image_type){
                case 'png':
                    $image = imagecreatefrompng($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, $src_x, $src_y, $max_size, $max_size, $src_w, $src_h);
                    imagepng($temp_image, $file_new);
                    break;
                case 'jpg':
                    $image = imagecreatefromjpeg($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, $src_x, $src_y, $max_size, $max_size, $src_w, $src_h);
                    imagejpeg($temp_image, $file_new);
                    break;
                case 'gif':
                    $image = imagecreatefromgif($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, $src_x, $src_y, $max_size, $max_size, $src_w, $src_h);
                    imagegif($temp_image, $file_new);
                    break;
            }
        }
    }


Top
 Profile  
 
PostPosted: Wed Feb 13, 2008 5:19 pm 
Offline
Forum Contributor
User avatar

Joined: Tue Jun 28, 2005 2:47 pm
Posts: 126
Location: Ljubljana, Slovenia


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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