Image resize in php
by kalai[ Edit ] 2010-02-10 17:31:24
Below code is used to fetch image from database , resize image to required size and insert into database
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
//$db_selected = mysql_select_db('withfriendship', $link);
$db_selected = mysql_select_db('imageddb', $link);
$qry="select slideid,filename,image from images where slideid in( select id from slide
where id<100) && position=1";
$max_width = 100;
$res=mysql_query($qry);
while($row = mysql_fetch_object($res)){
header("Content-type: image/gif");
$image = imagecreatefromstring($row->image);
$current_width = imagesx($image);
$current_height = imagesy($image);
// Set thumbnail width
$widths = array($current_width, $max_width);
$new_width = min($widths);
// Calculate thumbnail height from given width to maintain ratio
$new_height = $current_height / $current_width*$new_width;
// Create new image using thumbnail sizes
$thumb = imagecreatetruecolor($new_width,$new_height);
// Copy original image to thumbnail
imagecopyresampled($thumb,$image,0,0,0,0,$new_width,$new_height,imagesx($image),imagesy($image));
// Show thumbnail on screen
imagegif($thumb, "./temp/tfile"); //temp->temporary folder tfile ->temporary file inside temp folder
$nname = "./temp/tfile";
$fileo = fopen($nname,"rb");
$imgData = addslashes(file_get_contents($nname));
$qryy="insert into thumbnails values('',$row->slideid,'$row->filename','{$imgData}')";
$ress=mysql_query($qryy);
}
?>