Posted by: yuvan004 | July 23, 2010

HOW TO CHANGE IMAGE SIZE USING PHP?


share this

<?php
function convert_image($sname)
{
header(‘Content-type: image/jpeg,image/gif,image/png’);
//image size
list($width, $height) = getimagesize($sname);
$wid=450;
$hei=450;
$thumb = imagecreatetruecolor($wid,$hei);
//image type
$type=exif_imagetype($sname);
//check image type
switch($type)
{
case 2:
$source = imagecreatefromjpeg($sname);
break;
case 3:
$source = imagecreatefrompng($sname);
break;
case 1:
$source = imagecreatefromgif($sname);
break;
}
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0,$wid,$hei, $width, $height);
// source filename
$file = basename($sname);
//destiantion file path
$path=”/opt/lampp/htdocs/images/”;
$dname=$path.$wid.”x”.$hei.”_”.$file;
//display on browser
imagejpeg($thumb);
//store into file path
imagejpeg($thumb,$dname);
}
?>

this is example for image resizing in 200 width and 262 height…

you can also change the width and height you needed by change value of $wid and $hei..

it will help you in resize to what ever you want…

pass the source file name with their path to convert_image function

$sname–>Source file

$path–>Destination path..

it will generate destination file name automatically using source name..

for example if your file in htdocs specify $sname as /opt/lampp/htdocs/file_name.

$path is the place where you want to store.

GO TO THE TERMINAL

type  “sudo chmod -R 777 file name”

is necessary……

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories

%d bloggers like this: