Wednesday, July 29, 2009

Upload images with resize or thumb image in php

function add_picture_story($story_id,$filename,$tmpname) {
global $config;
$filename_parts = explode(".",strrev($filename),2);
$filename_base = strrev($filename_parts[1]);
$filename_ext = strrev($filename_parts[0]);

$unique_filename_base = strtolower(sanitize_filename($filename_base));
$final_filename = $unique_filename_base . "." . $filename_ext;

// final fully qualified file name
$final_fqfn = $config["basedir"].'images/images_story/'.$final_filename;

if (is_uploaded_file($tmpname)) {
if (!move_uploaded_file($tmpname,$final_fqfn)) {
$result['errors'] .= sprintf(plog_tr('Could not move uploaded file! %s to %s'),$tmpname,$final_fqfn);
return $result;
}
}

@unlink($tmpname);
$res = chmod($final_fqfn, 0755);
$destination= $config["basedir"].'images/images_story/thumb_story/'.$final_filename;
$imagename = $final_filename;
$height =111;
$width =158;
if(isset($filename) && trim($filename)!=""){
image_thumb($final_fqfn,$destination,$imagename,$height,$width);
}

$update_story = mysql_query("update plogger_stories SET image_story ='$final_filename' where id='$story_id'");

}



function image_thumb($filename,$destination,$imagename,$height,$width)
{

$length=strlen($imagename);
$newpos=strrpos($imagename,".")+1;
$type=substr($imagename,$newpos,$length-$newpos);
$imagetype=strtolower($type);
$th_height=$height;
$th_width=$width;
$new = imagecreatetruecolor($th_width, $th_height);
if(strtolower($imagetype)=="png")
{
$source = imagecreatefrompng($filename);
imagecopyresampled($new, $source, 0, 0, 0, 0, $th_width, $th_height,
imagesx($source), imagesy($source));
imagepng($new, $destination);
}
elseif(strtolower($imagetype)=="gif")
{
$source = imagecreatefromgif($filename);
imagecopyresampled($new, $source, 0, 0, 0, 0, $th_width, $th_height,
imagesx($source), imagesy($source));
imagegif($new, $destination);
}
else
{
$source = imagecreatefromjpeg($filename);
imagecopyresampled($new, $source, 0, 0, 0, 0, $th_width, $th_height,
imagesx($source), imagesy($source));
imagejpeg($new, $destination);
}


}

No comments:

Post a Comment