Page 1 of 1

Help with image resizing

Posted: Sat Dec 10, 2016 3:40 am
by Georgezx9
Ultimately I want to dynamically re-size images to fit cells in a table, and they may vary.

I have the following code that use the function to show a specific image (plan is for it to vary) however it does not show an image, it simply shows garbled hieroglyphics.

When I dot as a straightline code without the function it works well.

What am I doing wrong

thanks in advance

GY

Code: Select all

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

Re: Help with image resizing

Posted: Sat Dec 10, 2016 5:52 am
by requinix
1. You have an echo at the top of the function. That will mess up everything. Get rid of it.
2. You're calling show_resized_image with the string '$this_file'. Do you remember the rules about putting variables into strings?
3a. show_resized_image currently does not return anything. $target will be null.
3b. What it does do is output an image. Output. Meaning you have to use this script as the href of an <img> and not call it within the HTML markup.

Re: Help with image resizing

Posted: Sat Dec 10, 2016 7:03 am
by Georgezx9
Thanks, it now works, however your post has highlighted some issues that clearly I don't understand.

Why does having an echo affect the image being shown on screen

And what are the rules you mention on string variables.

GY

Re: Help with image resizing

Posted: Sat Dec 10, 2016 7:24 am
by requinix
Georgezx9 wrote:Why does having an echo affect the image being shown on screen
Quick overview of HTTP:
Every page that comes from a server has two parts: a header and a body. The header contains information about the content, like what type of content it is (HTML or an image), and the body is the raw data for that content. The header always comes before the body because the browser has to have the header for it to know what to do with the body.

In PHP you can manipulate the information in the header with, appropriately, the header() function. Your code uses it to specify that the type of content is a PNG image. But before that happened you tried to output something - which goes in the body. Since the headers must come first, PHP had to send all the headers so it could begin sending the output. By default the headers say that the content type is HTML, so that's what the output is going to be interpreted as. When the code tries to use header() later it was too late to modify the headers so PHP ignored it.

Actually, if your development environment was set up correctly then you would have seen that PHP tried to warn you about this happening: something like "Cannot send headers - output started at...". I assume you didn't see that so it means your environment is not set up correctly. You should look into the display_errors and error_reporting php.ini settings...

The code then went on and tried to output raw image data. The browser was expecting HTML so it tried to render the data as HTML. Thus the weird output.
Georgezx9 wrote:And what are the rules you mention on string variables.
Variables don't work in single-quoted strings.

Code: Select all

$var = "world";
echo 'Hello $var!';

Code: Select all

Hello $var!
They do work in double-quoted strings.

Code: Select all

$var = "world";
echo "Hello $var!";

Code: Select all

Hello world!
But you do not need to put quotes around variables by themselves. Writing "$var" is silly - just write $var.

You should look around for a good tutorial or two on PHP. I don't have any recommendations but apparently lots of people learn well from videos.

Re: Help with image resizing

Posted: Sat Dec 10, 2016 2:21 pm
by Georgezx9
Thank you so much, I was aware of the variable bit, but had become lazy.

The header bit, is something I had no inclination of.

GY