Topic: image resizing code for php

i'm proud of this.. took me a couple hours tonight for a client:

 **********************************************************************************
 * Copyright (c) 2007 Jonathan Lucas Reddinger <lucas@wingedleopard.net>          *
 *                                                                                *
 * Permission to use, copy, modify, and distribute this software for any          *
 * purpose with or without fee is hereby granted, provided that the above         *
 * copyright notice and this permission notice appear in all copies.              *
 *                                                                                *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES       *
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF               *
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR        *
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES         *
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN          *
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF        *
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.                 *
 **********************************************************************************


    function make_image($old_name, $width, $height) {
        // takes in image with filename $old_name
        // and return it in hex with new $width and $height
        $ratio = $width / $height;
        // pull in the tmp file as a jpg
        $img = imagecreatefromjpeg($old_name);
        // get the dimensions of the image
        list($src_w, $src_h) = getimagesize($old_name);
        $src_ratio = $src_w / $src_h;    // src ratio
        $new = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($new, 255, 255, 255);
        imagefill($new, 0, 0, $white);
        if ($src_w > $width || $src_h > $height) {    // we need to resample down!
            if ($src_ratio > $ratio) {        // src is wider
                // center vertically
                $rect_w = $width;
                $rect_h = $width / $src_ratio;
                $rect_x = 0;
                $rect_y = ($height - $rect_h)/2;
            } else if ($src_ratio < $ratio) {    // src is taller
                // center horizontally
                $rect_w = $height * $src_ratio;
                $rect_h = $height;
                $rect_x = ($width - $rect_w)/2;
                $rect_y = 0;
            } else {                // same proportions
                // do not have to center!
                $rect_w = $height * $src_ratio;
                $rect_h = $width / $src_ratio;
                $rect_x = 0;
                $rect_y = 0;
            };
            imagecopyresampled($new, $img, $rect_x, $rect_y, 0, 0, $rect_w, $rect_h, $src_w, $src_h);
        } else {
            // copy centered
            $rect_x = ($width - $src_w)/2;
            $rect_y = ($height - $src_h)/2;
            imagecopy($new, $img, $rect_x, $rect_y, 0, 0, $src_w, $src_h);
        };
        imagejpeg($new, "temp/img.jpg", 90);
        imagedestroy($new);
        // delete the original
        imagedestroy($img);
        // read the file into a string and delete it
        $str = file_get_contents("temp/img.jpg");
        unlink("temp/img.jpg");
        $hex = bin2hex($str);
        return $hex;
    };

Last edited by lucas (2009-07-07 02:00:32)

Re: image resizing code for php

Let's just hope no two users access that PHP script at the same time...

Re: image resizing code for php

yeah. i guess i could fix that issue but there's only one guy who is going to ever use it.

maybe i should fix it anyway in case he decided to go crazy and use a few instances at once.

thanks for reading it and telling me of a bug, maxlor!

Last edited by lucas (2007-02-20 06:08:20)

Re: image resizing code for php

is there an imagejpeg() equivalent that outputs to string instead of a file? that would also be great. i couldn't find such a function. sad

Re: image resizing code for php

maybe output buffering will do. use ob_start(); before imagejpeg call (which now outputs to buffer) and then retrieve the result with ob_get_contents(); then do finishing ob_end_clean(); and you should be done.

Re: image resizing code for php

awesome. thanks!