Forum Moderators: coopster

Message Too Old, No Replies

php image resize not working locally

         

Sandro87

11:53 pm on Feb 2, 2010 (gmt 0)

10+ Year Member



Hello
I'm using a plugin for TinyMCE (Archiv) that has an upload manager. What it does is it essentially uploads the image, move it to folder, resize to 1 big size, resize to 1 small size (thumb), destroy the original. In the remote server it works fine (apache, linux, php5) but not in my local machine (apache, windows, php5) and I don't understand why!
What it does in my local pc (for what I understand) is move the img to the folder (so the paths are ok since is all processed in one folder) and stop. It won't process the resizing.

the code is :


function addFile(){
# Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
header("HTTP/1.1 500 Internal Server Error");
$this->output(array('message'=>$this->language['ErrorInvalidUpload']));
}

# set filename and complete path
$path = $this->getFullPath($this->data['path']);
$file['name'] = $path.$_FILES["Filedata"]['name'];

# check if file exists
if(file_exists($file['name'])){
$this->output(array('message'=>$this->language['ErrorFileAlreadyExists']));
}

#move file to upload dir
$move = move_uploaded_file($_FILES["Filedata"]['tmp_name'], $file['name']);


#get the info of the image
list($width, $height) = @getimagesize($file['name']);
$mime_type = $this->returnMIMEType($file['name']);

#check if it is a picture
if(in_array($mime_type,$this->settings['allowed_image_mime']) && $this->data['browser']=="images"){
#picture
$img['p_width'] = $width;
$img['p_height'] = $height;

#thumb
$img['i_width'] = $img['p_width'];
$img['i_height'] = $img['p_height'];

#scale the image if needed
if(($img['p_width'] > $this->settings['max_image_size']) || ($img['p_height'] > $this->settings['max_image_size'])){
if($img['p_width'] > $img['p_height']){
$image_width = $this->settings['max_image_size'];
$image_height = round(($this->settings['max_image_size']/$img['i_width'])*$img['i_height']);
}
else{
$image_width = round(($this->settings['max_image_size']/$img['i_height'])*$img['i_width']);
$image_height = $this->settings['max_image_size'];
}

$img['p_width'] = $image_width;
$img['p_height'] = $image_height;
}

# resize the thumb
if($img['i_width'] > $img['i_height']){
$image_small_width = $this->settings['max_image_thumb_size'];
$image_small_height = round(($this->settings['max_image_thumb_size']/$img['i_width'])*$img['i_height']);
}
else{
$image_small_width = round(($this->settings['max_image_thumb_size']/$img['i_height'])*$img['i_width']);
$image_small_height = $this->settings['max_image_thumb_size'];
}

$img['i_width'] = $image_small_width;
$img['i_height'] = $image_small_height;

switch($mime_type){
case "image/jpg":
$this->create_jpg($file['name'], $width, $height, $img['p_width'], $img['p_height'], $img['i_width'], $img['i_height']);
break;

case "image/png":
$this->create_png($file['name'], $width, $height, $img['p_width'], $img['p_height'], $img['i_width'], $img['i_height']);
break;

case "image/gif":
$this->create_gif($file['name'], $width, $height, $img['p_width'], $img['p_height'], $img['i_width'], $img['i_height']);
break;

default:
break;
}
}
# else check if the file has the right mime-type
elseif(!in_array($mime_type,$this->settings['allowed_file_mime']) && $this->data['browser'] == "files" ||
!in_array($mime_type,$this->settings['allowed_image_mime']) && $this->data['browser']=="images"){
$unlink = unlink($file['name']);
$this->output(array('message'=>$this->language['ErrorWrongMimeType'].' `'.$mime_type.'`'));
}

$this->output(array('message'=>"ok"));
}

/**
* Create a resized image from a jpg file
* @param string $file the location of the image
* @param int $width the width of the origional image
* @param int $height the height of the origional image
* @param int $pic_new_width the new width for the image
* @param int $pic_new_heightthe new height for the image
* @param int $thumb_width the width for the thumb image
* @param int $thumb_heightthe height for the thumb image
* @return void
*/
function create_jpg($file, $width, $height, $pic_new_width, $pic_new_height, $thumb_width, $thumb_height){
#fetch the extention from the file
$file_path = substr($file,0,strrpos($file,DIRECTORY_SEPARATOR));
$file_name = substr($file,strlen($file_path)+1, (strrpos($file,".")-(strlen($file_path)+1)));
$file_ext= substr($file,strrpos($file,"."));

#create the picture
$pic = imagecreatetruecolor($pic_new_width, $pic_new_height);
$source = imagecreatefromjpeg($file);
$imgcpyrsmpld = imagecopyresampled( $pic, $source, 0, 0, 0, 0, $pic_new_width, $pic_new_height, $width, $height );
$imagejpeg = imagejpeg($pic, $file_path.DIRECTORY_SEPARATOR.$file_name.$file_ext, 100);
$imagedestroy = imagedestroy($pic);

list($width, $height) = @getimagesize($file);

#create the thumb
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
$source2 = imagecreatefromjpeg( $file );
$imgcpyrsmpld2 = imagecopyresampled( $thumb, $source2, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );
$imagejpeg2 = imagejpeg($thumb, $file_path.DIRECTORY_SEPARATOR.$file_name.'_thumb'.$file_ext, 100);
$imagedestroy2 = imagedestroy($thumb);
}

/**
* Create a resized image from a gif file
* @param string $file the location of the image
* @param int $width the width of the origional image
* @param int $height the height of the origional image
* @param int $pic_new_width the new width for the image
* @param int $pic_new_heightthe new height for the image
* @param int $thumb_width the width for the thumb image
* @param int $thumb_heightthe height for the thumb image
* @return void
*/
function create_gif($file, $width, $height, $pic_new_width, $pic_new_height, $thumb_width, $thumb_height){
#fetch the extention from the file
$file_path = substr($file,0,strrpos($file,DIRECTORY_SEPARATOR));
$file_name = substr($file,strlen($file_path)+1, (strrpos($file,".")-(strlen($file_path)+1)));
$file_ext= substr($file,strrpos($file,"."));

#create the picture
$pic = imagecreatetruecolor($pic_new_width, $pic_new_height);
$source = imagecreatefromgif($file);
$imgcpyrsmpld = imagecopyresampled( $pic, $source, 0, 0, 0, 0, $pic_new_width, $pic_new_height, $width, $height );
$imagegif = imagegif($pic, $file_path.DIRECTORY_SEPARATOR.$file_name.$file_ext);
$imagedestroy = imagedestroy($pic);

list($width, $height) = @getimagesize($file);

#create the thumb
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
$source2 = imagecreatefromgif( $file );
$imgcpyrsmpld2 = imagecopyresampled( $thumb, $source2, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );
$imagegif2 = imagegif($thumb, $file_path.DIRECTORY_SEPARATOR.$file_name.'_thumb'.$file_ext);
$imagedestroy2 = imagedestroy($thumb);
}

/**
* Create a resized image from a png file
* @param string $file the location of the image
* @param int $width the width of the origional image
* @param int $height the height of the origional image
* @param int $pic_new_width the new width for the image
* @param int $pic_new_heightthe new height for the image
* @param int $thumb_width the width for the thumb image
* @param int $thumb_heightthe height for the thumb image
* @return void
*/
function create_png($file, $width, $height, $pic_new_width, $pic_new_height, $thumb_width, $thumb_height){
#fetch the extention from the file
$file_path = substr($file,0,strrpos($file,DIRECTORY_SEPARATOR));
$file_name = substr($file,strlen($file_path)+1, (strrpos($file,".")-(strlen($file_path)+1)));
$file_ext= substr($file,strrpos($file,"."));

#create the picture
$pic = imagecreatetruecolor($pic_new_width, $pic_new_height);
$source = imagecreatefrompng($file);
$imgcpyrsmpld = imagecopyresampled( $pic, $source, 0, 0, 0, 0, $pic_new_width, $pic_new_height, $width, $height );

if(version_compare(PHP_VERSION, '5.1.2', '<')){
$imagepng = imagepng($pic, $file_path.DIRECTORY_SEPARATOR.$file_name.$file_ext);
}
else{
$imagepng = imagepng($pic, $file_path.DIRECTORY_SEPARATOR.$file_name.$file_ext, 0);
}

$imagedestroy = imagedestroy($pic);

list($width, $height) = @getimagesize($file);

#create the thumb
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
$source2 = imagecreatefrompng( $file );
$imgcpyrsmpld2 = imagecopyresampled( $thumb, $source2, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );

if(version_compare(PHP_VERSION, '5.1.2', '<')){
$imagepng2 = imagepng($thumb, $file_path.DIRECTORY_SEPARATOR.$file_name.'_thumb'.$file_ext);
}
else{
$imagepng2 = imagepng($thumb, $file_path.DIRECTORY_SEPARATOR.$file_name.'_thumb'.$file_ext, 0);
}

$imagedestroy2 = imagedestroy($thumb);
}


I tried to understand but with no success. Since it's called with tinyMCE and jquery it wont show PHP errors in the page so I enabled the log error in PHP but the log is empty = no errors!
How can I debug this? Or what could the cause be?

coopster

1:18 pm on Feb 26, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you certain you have enabled image manipulation in the php.ini?

Sandro87

1:45 pm on Feb 26, 2010 (gmt 0)

10+ Year Member



yes my personal tests work