Forum Moderators: coopster

Message Too Old, No Replies

Equal ratio image with netpbm

         

pcmodeler

12:55 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



I'm trying to modify a script I am running for a gallery application. The script creates thumbnails properly, but only changes it based on width, which is a variable supplied in a sql table. I'm trying to modify the script so that it so that the width "and height" are no greater than that variable. Here is what I have so far:

code:--------------------------------------------------------------------------------
<?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;
list($newHeight) = $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, $newHeight); break;
default: return $src_file; break;
}
}

function RatioResizeImgNetPBM($src_file, $dest_file, $newWidth, $newHeight) {
/* Define where to find the various external binaries we need */
$netpbm = megConfigGetVar('netpbmPath'); /* default: home/www/pcmodeler/netpbm */

$djpeg =$netpbm."/djpeg"; /* decompresses a jpeg to ppm */
$cjpeg =$netpbm."/cjpeg"; /* compreses a ppm to jpeg format */
$pnmscale = $netpbm."/pnmscale"; /* scales a ppm image */
$pnmscale = $netpbm."/pnmscale"; /* scales a ppm image */
$jpgtopnm = $netpbm."/jpegtopnm"; /* convert a jpg to ppm */
$giftopnm = $netpbm."/giftopnm"; /* convert a gif to ppm */
$pngtopnm = $netpbm."/pngtopnm"; /* convert a png to ppm */
$ppmtojpg = $netpbm."/ppmtojpeg"; /* convert a ppm to jpg */
$ppmtogif = $netpbm."/ppmtogif"; /* convert a ppm to gif */
$ppmquant = $netpbm."/ppmquant"; /* colour quantize a ppm */

/* $imginfo = @getimagesize($src_file);
if ($galleryvar['imageSoftwarePath'] == '')
$cmd = $galleryvar['imageSoftwarePath']."/";
else
$cmd = "";
*/

if(!file_exists($dest_file)) {
if(ereg("\.gif$",$src_file)) { /* Look for .gif extension */
exec("$giftopnm $src_file ¦ $pnmscale -width $newWidth ".
"¦ $ppmquant 256 ¦ $ppmtogif -interlace > \"$dest_file\"");
} elseif(ereg("\.jpe?g",$src_file)) { /* Look for .jpg or .jpeg */
exec("$jpgtopnm $src_file ¦ $pnmscale -xysize $newWidth $newHeight".
"¦ $ppmtojpg > \"$dest_file\"");
} elseif(ereg("\.JPE?G",$src_file)) { /* Look for .JPG or .JPEG */
exec("$jpgtopnm $src_file ¦ $pnmscale -xysize $newWidth $newHeight".".
"¦ $ppmtojpg > \"$dest_file\"");
} elseif(ereg("\.png",$src_file)) { /* Look for .png */
exec("$pngtopnm $src_file ¦ $pnmscale -xysize $newWidth $newHeight".¦ ".
"¦ $ppmquant 256 ¦ $pngtopnm > \"$dest_file\"");
} else { /* not a GIF, PNG or JPG file */
return("");
}
}
return $dest_file;
}

?>
--------------------------------------------------------------------------------

So far, I can't get it to modify the width, so that portrait images are no larger than the variable. What am I missing?

jatar_k

5:21 pm on Jun 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



exec("$giftopnm $src_file ¦ $pnmscale -width $newWidth ". "¦ $ppmquant 256 ¦ $ppmtogif -interlace > \"$dest_file\"");
exec("$jpgtopnm $src_file ¦ $pnmscale -xysize $newWidth $newHeight". "¦ $ppmtojpg > \"$dest_file\"");
exec("$pngtopnm $src_file ¦ $pnmscale -xysize $newWidth $newHeight".¦ " . "¦ $ppmquant 256 ¦ $pngtopnm > \"$dest_file\"");

I assume these three lines are the true issue (as well as the uppercase jpeg line)? I don't use netpbm so I don't really know the structure of the calls. Did you add just newHeight or were both newWidth and newHeight added to these calls?

pcmodeler

5:43 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



I just added newHeight.

The reason for the uppercase call was because it was working correctly when a file extension was in uppercase.

I agree, that I think those three lines are the issue, but like yourself, I'm not sure of the proper call with netpbm.

pcmodeler

6:21 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



I cheated. I think the problem was because newHieght is not decalared as a value.

I redeclared newWidth as the y axis and it worked.

exec("$jpgtopnm $src_file ¦ $pnmscale -xysize $newWidth $newWidth". "¦ $ppmtojpg > \"$dest_file\"");

It's sloppy, but it works.

jatar_k

6:39 pm on Jun 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



as long as it works then it can gat pretty later ;)

glad you sorted it.