Image resize not working

A

Anonymous

Guest
The code below does not display an image it simply puts a load of hieroglyphic on the screen as if its the bytes of data, but not formatted as a picture.

Initially it was a simple straight code with out it being a function and it works, but in the function it does not, any help greatly appreciated.

GY
Code:
<?php
function show_resized_image($filepath){
echo $filepath,"??";
/*
	* PHP GD
	* resize an image using GD library
*/

// File and new size
//the original image has 800x600
//$filepath='images/fam3-3.JPG';

//the resize will be a percent of the original size
	$percent = 0.25;



// Get new sizes
	list($width, $height) = getimagesize($filepath);
	$newwidth = $width * $percent;
	$newheight = $height * $percent;
// Content type
	header('Content-Type: image/jpeg');
// Load
	$thumb = imagecreatetruecolor($newwidth, $newheight);
	$source = imagecreatefromjpeg($filepath);

// Resize
	imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output and free memory
//the resized image will be 400x300
	
	imagejpeg($thumb);
	imagedestroy($thumb);
	return;
}

	$user_name = "root";
	$password = "";
	$database = "photos";
	$server = "localhost";
	$photoid= 55;
	$SQL = "SELECT * FROM photosdata where ID=$photoid";
	
	$db_handle = mysqli_connect($server, $user_name, $password,$database);
	$result = mysqli_query($db_handle,$SQL);
	$db_field=mysqli_fetch_assoc($result);
	
	$fname=$db_field['photoname'];
	$path=$db_field['path'];
	//echo $path.$fname;
/*	
echo $path.$match["photoname"];
echo '<img src="'.$path.$match["photoname"].'" />';
mysqli_close($db_handle);
*/	
	$this_file=$path.$fname;
	
	$target=show_resized_image('$this_file');
	//$target=show_resized_image('images/fam3-3.JPG');
?>
 
You should make two separated scripts. The first should render a markup, the second should generate resized images.
That is something like this:
Code:
<?php
//render.php

//load the image from the db
...
echo '<img src="resize_image.php?filepath="'.urlencode($loadedImageFromDb).'">';
...
?>

Code:
<?php
//resize_image.php

//!!! it is IMPORTANT here must not be any output except "header()" and "imagejpeg()"

//you can create a function isFileEligible and check e.g. whether the file has a right extension, lies in the right place 
//to prevent hacker attacks
if (!isset($_GET['filepath']) || !isFileEligible($_GET[file'path'])) {
  exit();
}

$filepath = $_GET['filepath'];

//your resize logic
...
// Content type
header('Content-Type: image/jpeg');
imagejpeg($thumb);
imagedestroy($thumb);
?>
 
Back
Top