Follow Me

Simple image resize script in php

Thu, May 20, 2010

PHP

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 :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • 6 Comments For This Post

    1. Ravi Says:

      Very Nice script

      Thanks
      JeevanSathi

    2. Kim Says:

      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);

    3. Kim Says:

      btw tried with 100 instead of 160. but still same on png images.

    4. Kim Says:

      nvm im just stupid. Wrong numbers and wrong files haha :) Still good script :P

    5. admin Says:

      @Kim, Thanks and Congrats :)

    6. admin Says:

      @Ravi Thank you

    Leave a Reply