Jahanzaib Asgher
Jahanzaib Asgher

Reputation: 252

PHP image resize isn't working

I found the following script from PHP site to half the size of the image. I use the database to fetch the link of the image. Other things are working correctly which means there isn't any kind of error anywhere except this one.

echo "<img src='".// File and new size
$filename = '$row["image"]';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

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

// Output
imagejpeg($thumb);
"'>"

Error: The whole page is destroyed and there is a broken link of an image.
Hope you guys will help me!

Upvotes: 0

Views: 50

Answers (2)

Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4028

Try this it works

<?php

$filename = '$row["image"]';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
 $newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

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

// Output

echo "<img src='".imagejpeg($thumb)."'>";
?>

Upvotes: 1

n-dru
n-dru

Reputation: 9430

It works after you drop those lines:

echo "<img src='".// File and new size

"'>"

header does it for you, informs that there is jpg coming, no need to echo image tag.

The other solution is to remove this line:

header('Content-Type: image/jpeg');

And create new file, then use it as a source of an image:

// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";

Upvotes: 1

Related Questions