Page 1 of 1

Image Resize Problem

Posted: Sun Dec 01, 2002 7:13 pm
by crondeau
Hi there,

I downloaded the mImage class from phpclasses.org and I'm having difficulty understanding why it is now working. These files allow to select an image in a directory and resize them using netpdbm. Only two files are needed.

The first one is as follows:

<?php

require('mImage_class.php');

$myImage = new mImage("images/someimage.jpg");
$myImage->create_thumbnail();
$myImage->resize();
?>

and the second:

<?php

define(NETPBM_DIR , "/util/netpbm/");
define(TH_MAX_HEIGHT , "100");
define(TH_MAX_WIDTH , "100");
define(IMG_MAX_HEIGHT, "150");
define(IMG_MAX_WIDTH, "300");

define(MIMAGE_TEST, "false");

class mImage{

var $filename;
var $tmp_file;
var $th_tmp_file;
var $factor;
var $type;
var $new_file;
var $thumbnail;
var $height;
var $width;

function mImage($filename="-1"){

// Test to see if filename was given in object creation
if($filename == "-1"){
echo "ERROR: You must use a filename in new mImage()\n";
exit;
}

// Test filename type only accept jpeg and gif
if( !(eregi("\.(jpg|jpeg|gif)$",$filename)) ){
echo "file[$filename] is an invalid type.\n";
exit;
}


// Test existence
if(!(file_exists($filename))){
echo "file[$filename] does not exist.\n";
exit;
}

// Get Size of Image
$sizes = getimagesize($filename);
$this->width = $sizes[0];
$this->height = $sizes[1];

if(($this->width<=0) || ($this->height<=0)){
echo "ERROR: could not get size of image[$filename]\n";
exit;
}

// Random number is used to create temporary filenames
srand((double) microtime() * 1000000);
$random = rand();

$this->filename = $filename;
$parts = split("\.",$filename);
$this->type = $parts[(count($parts)-1)];

$parts = split("/",$filename);
for($i=1; $i<count($parts); $i++){
($i==(count($parts)-1)) ? $fname = $parts[$i] :$dname .= "/".$parts[$i];
}

$this->thumbnail = $dname . "/th_".$fname;
$this->tmp_file = $dname . "/". $random . ".pnm";
$this->th_tmp_file = $dname . "/th_". $random . ".pnm";

if(MIMAGE_TEST){
echo "filename[".$this->filename."]\n";
echo "directory = $dname\n";
echo "filename = $fname<br>\n";
echo "thumbnail[".$this->thumbnail."]\n";
echo "tmp_file[".$this->tmp_file."]\n";
echo "th_tmp_file[".$this->th_tmp_file."]\n";
echo "type[".$this->type."]\n";
}
}

// resize() - resize the file if the image is larger then the globals
// IMG_MAX_WIDTH and IMG_MAX_HEIGHT

function resize(){

// If image is smaller then max size there is nothing to do
if( ($this->width <= IMG_MAX_WIDTH) && ($this->height <= IMG_MAX_HEIGHT) ){
return;
}

// Determine the proper netbpm command to use given the $this->type of image
(eregi("(jpeg|jpg)",$this->type))? $pnm_cmd = "jpegtopnm" : $pnm_cmd = "giftopnm";
$tmp_cmd = NETPBM_DIR . $pnm_cmd . " " . $this->filename . " > ". $this->tmp_file;


// If we resize using the largest dimension is the NEW smaller dimension
// still too big?
// no - then resize using the largest dimension
// yes - then resize using the smaller dimension
if($this->width > $this->height){
$new_height = (IMG_MAX_WIDTH/$this->width) * $this->height;
if($new_height > IMG_MAX_HEIGHT){
$resize_cmd = NETPBM_DIR. "pnmscale -height=" . IMG_MAX_HEIGHT ." ".$this->tmp_file." > ".$this->th_tmp_file;
}else{
$resize_cmd = NETPBM_DIR. "pnmscale -width=". IMG_MAX_WIDTH ." ".$this->tmp_file." > ".$this->th_tmp_file;
}
}else{

$new_width = (IMG_MAX_HEIGHT/$this->height) * $this->width;

if($new_width > IMG_MAX_WIDTH){

$resize_cmd = NETPBM_DIR. "pnmscale -width=". IMG_MAX_WIDTH ." ".$this->tmp_file." > ".$this->th_tmp_file;

}else{

$resize_cmd = NETPBM_DIR. "pnmscale -height=" . IMG_MAX_HEIGHT ." ".$this->tmp_file." > ".$this->th_tmp_file;

}

}

$last_cmd = NETPBM_DIR. "ppmtojpeg ". $this->th_tmp_file ." > ".$this->filename;

$junk = `$tmp_cmd`;
$junk = `$resize_cmd`;
$junk = `$last_cmd`;

// Re-calculate width and height properties

$sizes = getimagesize($this->filename);
$this->width = $sizes[0];
$this->height = $sizes[1];


if(file_exists($this->tmp_file)){
unlink($this->tmp_file);
}

if(file_exists($this->th_tmp_file)){
unlink($this->th_tmp_file);
}

}

// create_thumbnail($thumbnail_filename)
// $thumbnail_filename is optional
// If its passed in it should be the name of the thumbnail that will be created
// If its not used the default thumbail name is th_($this->filename)

function create_thumbnail($thumbnail="-1"){
if($thumbnail != "-1"){
$this->thumbnail = $thumbnail;
}

// if image is already smaller then the a thumbnail just copy it
if( ($this->width <= TH_MAX_WIDTH) && ($this->height <= TH_MAX_HEIGHT) ){
$resize_cmd = "/bin/cp ".$this->filename." ".$this->thumbnail;
system($resize_cmd);
return;
}

// Determine the proper netbpm command to use given the $this->type of image

(eregi("(jpeg|jpg)",$this->type))? $pnm_cmd = "jpegtopnm" : $pnm_cmd = "giftopnm";
$tmp_cmd = NETPBM_DIR . $pnm_cmd . " " . $this->filename . " > ". $this->tmp_file;


// If we resize using the largest dimension is the NEW smaller dimension
// still too big?
// no - then resize using the largest dimension
// yes - then resize using the smaller dimension

if($this->width > $this->height){
$new_height = (TH_MAX_WIDTH/$this->width) * $this->height;
if($new_height > TH_MAX_HEIGHT){
$resize_cmd = NETPBM_DIR. "pnmscale -height=" . TH_MAX_HEIGHT ." ".$this->tmp_file." > ".$this->th_tmp_file;
}else{
$resize_cmd = NETPBM_DIR. "pnmscale -width=". TH_MAX_WIDTH ." ".$this->tmp_file." > ".$this->th_tmp_file;
}

}else{

$new_width = (TH_MAX_HEIGHT/$this->height) * $this->width;
if($new_width > TH_MAX_WIDTH){
$resize_cmd = NETPBM_DIR. "pnmscale -width=". TH_MAX_WIDTH ." ".$this->tmp_file." > ".$this->th_tmp_file;
}else{

$resize_cmd = NETPBM_DIR. "pnmscale -height=" . TH_MAX_HEIGHT ." ".$this->tmp_file." > ".$this->th_tmp_file;
}
}


$thumb_cmd = NETPBM_DIR. "ppmtojpeg ". $this->th_tmp_file ." > ".$this->thumbnail;

$junk = `$tmp_cmd`;
$junk = `$resize_cmd`;
$junk = `$thumb_cmd`;

if(file_exists($this->tmp_file)){
unlink($this->tmp_file);
}
if(file_exists($this->th_tmp_file)){
unlink($this->th_tmp_file);
}
}
}
?>

The result of running this script gives me this:

filename[images/someimage.jpg]
directory = filename = someimage.jpg
thumbnail[/th_someimage.jpg]
tmp_file[/345.pnm]
th_tmp_file[/th_345.pnm]

and then the following error:

Warning: getimagesize: Read error! in C:\FoxServ\www\ImageManipulation\mImage_class.php on line 148

When trying to carry out the second getimagesize function, the image in the images folder no longer has an extension and is now reads 0Kb. I' m not sure what is going on here. Anyone have any idea?

Thanks :?:
type[jpg]

Posted: Mon Dec 02, 2002 1:35 am
by 9902468
1. when posting code place it between code tags.
2. you downloaded it from phpclasses? any docs there any help there?

-9902468

Posted: Mon Dec 02, 2002 3:26 am
by serg4444
Use this one - much easy :D

$ext=getimagesize($file);
if($ext[2]==1){$e=".gif";$src=imagecreatefromgif($file);}
if($ext[2]==2){$e=".jpg";$src=imagecreatefromjpeg($file);}
$image=$ImageName."_".$uid.$e;
$view="./ima/".$image;

$tn_w=80;
$tn_h = floor($tn_w * imagesy($src) / imagesx($src));
$dst = imagecreate($tn_w,$tn_h);
imagecopyresized($dst,$src,0,0,0,0,$tn_w,$tn_h,imagesx($src),imagesy($src));
$ima=imagejpeg($dst,$view,80);