Forum Moderators: coopster

Message Too Old, No Replies

gd image resize - scipt modification help needed

gd image resize

         

pcmodeler

5:31 pm on May 1, 2003 (gmt 0)

10+ Year Member



I'm currently using the following script to resize and create thumbnails for my online gallery. It resizes the width while keeping the aspect ration the same.


<?php
function RatioResizeImg($src_file, $dest_file, $gid) {

list($dbconn) = pnDBGetConn();
$pntable = pnDBGetTables();
$column = &$pntable['meg_categories_column'];
$query = "SELECT $column[thumbwidth] from $pntable[meg_categories] WHERE gallid=$gid";
$result = $dbconn->Execute($query);
list($newWidth) = $result->fields;

switch(megConfigGetVar('imageSoftware')) {
case 'GD': return RatioResizeImgGD($src_file, $dest_file, $newWidth); break;
case 'ImageMagick': return RatioResizeImgImageMagick($src_file, $dest_file, $newWidth); break;
case 'none': return $src_file; break;
case 'browser': return 'browser'; break;
case 'NetPBM': return RatioResizeImgNetPBM($src_file, $dest_file, $newWidth); break;
default: return $src_file; break;
}
}

// GD Library
function RatioResizeImgGD($src_file, $dest_file, $newWidth) {

// find the image size & type
$testGD = get_extension_funcs("gd"); // Grab function list
if (!$testGD){ return $src_file; /*echo "GD not even installed."; exit;*/ }
if (in_array ("imagegd2",$testGD))
$gd_version = "<2"; // Check

$imginfo = @getimagesize($src_file);
switch($imginfo[2]) {
case 1: $type = IMG_GIF; break;
case 2: $type = IMG_JPG; break;
case 3: $type = IMG_PNG; break;
case 4: $type = IMG_WBMP; break;
default: return $src_file; break;
}

switch($type) {
case IMG_GIF:
if(!function_exists('imagecreatefromgif')){return $src_file;}
$srcImage = @imagecreatefromgif("$src_file");
break;
case IMG_JPG:
if(!function_exists('imagecreatefromjpeg')){return $src_file;}
$srcImage = @ImageCreateFromJpeg($src_file);
break;
case IMG_PNG:
if(!function_exists('imagecreatefrompng')){return $src_file;}
$srcImage = @imagecreatefrompng("$src_file");
break;
case IMG_WBMP:
if(!function_exists('imagecreatefromwbmp')){return $src_file;}
$srcImage = @imagecreatefromwbmp("$src_file");
break;

default: return $src_file;
}

if($srcImage){
// height/width
$srcWidth = $imginfo[0];
$srcHeight = $imginfo[1];
$ratioWidth = $srcWidth/$newWidth;
$destWidth = $newWidth;
$destHeight = $srcHeight / $ratioWidth;

// resize
if ($gd_version == "<2")
{
$destImage = @imagecreate($destWidth, $destHeight);
imagecopyresized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
}
else
{
$destImage = @imagecreatetruecolor($destWidth, $destHeight);
imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
}
// create and save final picture

switch($type){
case IMG_GIF: @imagegif($destImage, "$dest_file"); break;
case IMG_JPG: @imagejpeg($destImage, "$dest_file"); break;
case IMG_PNG: @imagepng($destImage, "$dest_file"); break;
case IMG_WBMP: @imagewbmp($destImage, "$dest_file"); break;
}

// free the memory
@imagedestroy($srcImage);
@imagedestroy($destImage);

return $dest_file;
}
else
{
return $src_file;
}
}

I'd like to modify it to resize both height and width and keep the resize ration the same. In other words, resize it so that it is no larger than the pixel size specified in my database (which is 120).

I've examined a few example codes but am not quite sure beyond fully modifying this script how to change it. I did not write this original script.

Thanks for any help.

Birdman

5:55 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>I'd like to modify it to resize both height and width and keep the resize ration the same.

From what I can tell, that's exactly what should happen in your script. It looks right to me.

What is going wrong?

pcmodeler

6:02 pm on May 1, 2003 (gmt 0)

10+ Year Member



It's only resizing the width to 120, so if I have an image (480x640), the resulting thumbnail is 120x160. I need the image to be 80x120.

pcmodeler

6:04 pm on May 1, 2003 (gmt 0)

10+ Year Member



I'm guessing in need something similar to this in there:


if $srcWidth > $srcHeight then (follow this procedure) else....

Birdman

6:28 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace the following three lines with the code that follows it:

Replace this:
$ratioWidth = $srcWidth/$newWidth;
$destWidth = $newWidth;
$destHeight = $srcHeight / $ratioWidth;

...with this
if($srcWidth > $srcHeight){
$ratio = $srcWidth/$newWidth;
$destWidth = $newWidth;
$destHeight = $srcHeight / $ratio;
} else {
$ratio = $srcHeight/$newWidth;
$destHeight = $newWidth;
$destWidth = $srcWidth/$ratio;
}

You were on the right track!

I love using that function for uploading pics to my dynamic sites. Of course mine is about a tenth of that size because I wrote it by hand. Yours is made to be universal...hence the code bloat.

toadhall

6:38 pm on May 1, 2003 (gmt 0)

10+ Year Member



Hehe

My code paste has a "square" option:


$ratioLand = $srcWidth/$newWidth; // Landscape orientation
$ratioPort = $srcHeight/$newWidth; // Portrait orientation

if ($srcWidth>$srcHeight){ // a landscape
$destWidth = $newWidth;
$destHeight = $srcHeight/$ratioLand;
} elseif ($srcHeight>$srcWidth) { // a portrait
$destHeight = $newWidth;
$destWidth = $srcWidth/$ratioPort;
} else { // a square
$destWidth = $newWidth;
$destHeight = $newWidth;
}

T

Birdman

6:44 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah! Good thinking toadhall.

My altered code:

if($srcWidth => $srcHeight){
$ratio = $srcWidth/$newWidth;
$destWidth = $newWidth;
$destHeight = $srcHeight / $ratio;
} else {
$ratio = $srcHeight/$newWidth;
$destHeight = $newWidth;
$destWidth = $srcWidth/$ratio;
}

toadhall

6:49 pm on May 1, 2003 (gmt 0)

10+ Year Member



Now yer loggin'!

T

pcmodeler

7:04 pm on May 1, 2003 (gmt 0)

10+ Year Member



Thanks guys. That's what I thought I needed to do.

Hey Birdman, doesn't the first line actually need to be:


if($srcWidth [b]>=[/b] $srcHeight){

Not sure if that usually matters, but my script errored out when reversed.

Birdman

7:09 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I suppose you're right..I guess I wasn't really loggin'!

pcmodeler

7:19 pm on May 1, 2003 (gmt 0)

10+ Year Member



Not trying to condemn, just questioning if it matters in all cases. I'm a bit new to coding and am more of a deconstructor than a constructor. I can figure out what things do, but would not be able to write an entire script from scratch.

jatar_k

7:27 pm on May 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



By the way pcmodeler,
Welcome to WebmasterWorld, the lads seemed to have overlooked it ;)

I think we all started as deconstructors, it eventually becomes clear after that.

toadhall

7:36 pm on May 1, 2003 (gmt 0)

10+ Year Member



Sorry, manners go out de door right along witchez when you go off livin' in de woods...

Welcome to the Enchanted Forest pcmodeler.

t (humbled and cap in hand)

pcmodeler

7:51 pm on May 1, 2003 (gmt 0)

10+ Year Member



Thanks guys. I actually found it by doing a search on "gd image resize". Seemed it was the most useful of those sites that I found.

Birdman

8:09 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry 'bout that, WELCOME TO WW!

Yes it very handy. I built mine from snippets at php.net and actually posted one version in the PHP Bag 'O Tricks II [webmasterworld.com].