Forum Moderators: coopster
<?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.
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.
My code paste has a "square" option:
$ratioLand = $srcWidth/$newWidth; // Landscape orientation
$ratioPort = $srcHeight/$newWidth; // Portrait orientationif ($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
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].