This is very simple script for image resizing in php. You can need to call a simple function just before you submit your content to database.
Lets start. Suppose you have file input field like this.
<form method='post'> <input type="file" name="files" id="files" /> <input type="submit" name="submit" value="submit"> </form>
Now after you submit your form, you just need to put this code there.
<?php
if($_FILES['files']['name']!='')
{
$target_path = "images/"; // main image path
$filename=$_FILES['files']['name'];
$target_path1 = $target_path.basename($filename);
if(move_uploaded_file($_FILES['files']['tmp_name'], $target_path1))
{
// get file extension
$name_part=explode(".",$_FILES['files']['name']);
$ext=strtolower($name_part[(count($name_part)-1)]);
$logo_width="175";
$logo_height="94";
// thumbnail path here
$thumb_path="images/thumb/".$filename;
copy($target_path1,$thumb_path);
// just call image resize function
resize_picture($logo_width, $logo_height, $thumb_path, $ext);
mysql_query(" ---- your update query here----");
}
}
// resize_picture function definition here
function resize_picture($w, $h, $picture, $format)
{
$format = str_replace(".", "", $format);
switch(strtolower($format))
{
case "jpg":
//$th_size = new_picture_size($w, $h, $picture);
$oldpic = imagecreatefromjpeg($picture);
$newpic = imagecreatetruecolor($w, $h);
$size = min(imageSX($oldpic), imageSY($oldpic));
$offsetX = (imageSX($oldpic) - $size) / 2;
imagecopyresampled($newpic, $oldpic, 0, 0, $offsetX, 0, $w, $h, $size, $size);
imagejpeg($newpic, $picture, 100);
imagedestroy($oldpic);
imagedestroy($newpic);
break;
case "jpeg":
$oldpic = imagecreatefromjpeg($picture);
$newpic = imagecreatetruecolor($w, $h);
$size = min(imageSX($oldpic), imageSY($oldpic));
$offsetX = (imageSX($oldpic) - $size) / 2;
imagecopyresampled($newpic, $oldpic, 0, 0, $offsetX, 0, $w, $h, $size, $size);
imagejpeg($newpic, $picture, 100);
imagedestroy($oldpic);
imagedestroy($newpic);
break;
case "png":
$oldpic = imagecreatefrompng($picture);
$newpic = imagecreatetruecolor($w, $h);
$size = min(imageSX($oldpic), imageSY($oldpic));
$offsetX = (imageSX($oldpic) - $size) / 2;
//$offsetY = (imageSX($oldpic) - $size) / 2;
imagecopyresampled($newpic, $oldpic, 0, 0, $offsetX, 0, $w, $h, $size, $size);
imagepng($newpic, $picture, 100);
imagedestroy($oldpic);
imagedestroy($newpic);
break;
case "gif";
$oldpic = imagecreatefromgif($picture);
$newpic = imagecreate($w, $h);
$size = min(imageSX($oldpic), imageSY($oldpic));
$offsetX = (imageSX($oldpic) - $size) / 2;
//$offsetY = (imageSX($oldpic) - $size) / 2;
imagecopyresampled($newpic, $oldpic, 0, 0, $offsetX, 0, $w, $h, $size, $size);
imagegif($newpic, $picture, 100);
imagedestroy($oldpic);
imagedestroy($newpic);
break;
}
}
Hope this simple image resize script will help you to resolve your image resize related problems.
Cheers

March 30th, 2011 at 6:26 am
Very Nice script
Thanks
JeevanSathi
April 12th, 2011 at 12:03 am
Nice script. but
There is some error when i use some PNG images
gd-png: fatal libpng error: zlib error. on line 1023
Line 1022: imagecopyresampled($newpic, $oldpic, 0, 0, $offsetX, 0, $w, $h, $size, $size);
Line 1023: imagepng($newpic, $picture, 160);
Line 1024: imagedestroy($oldpic);
April 12th, 2011 at 12:04 am
btw tried with 100 instead of 160. but still same on png images.
April 12th, 2011 at 12:12 am
nvm im just stupid. Wrong numbers and wrong files haha
Still good script
April 21st, 2011 at 7:13 pm
@Kim, Thanks and Congrats
April 21st, 2011 at 7:13 pm
@Ravi Thank you