Page 1 of 1

Resize Image from MYSQL Database

Posted: Tue Jun 28, 2005 10:59 am
by blkrt10
Hi, I am storing images in a database and below is the fetch script to display them. This script SHOULD resize the image to half and output a jpg stream. The getimagesize does not work (thus, the other functions fail), because it is designed to handle a filename, not the actual binary (my $img variable)...but im not sure how to fix it. Any ideas?

Thanks in advance.

Code: Select all

<?php
if(isset($_GET['car_id']) && isset($_GET['image_num']))
{
       $car_id = $_GET['car_id'];
       $img_num= 'img';
       $img_num .= $_GET['image_num'];
	//Get Image
       require_once ('dbconnect.php');
	$sql = "select $img_num from auto where id=$car_id";
	$imgresult = mysql_query("$sql");
	
	header("Content-type: image/jpeg;");
	$img = mysql_result($imgresult, 0, $img_num);
        
	$percent = 0.5;
	list($width, $height) = getimagesize($img);
	$new_width= $width * $percent;
	$new_height = $height * $percent;
	
	//Resample
	$image_p = imagecreatetruecolor($new_width, $new_height);
	$image = imagecreatefromjpeg($img);
	imagecopyresampled($image_p, $image, 0,0,0,0, $new_width, $new_height, $width, $height);
	imagepeg($image_p, null, 100);
}
?>