Discuss Need GD help - output is gibberish in the PHP Development forum on Dev Shed. Need GD help - output is gibberish PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 215
Time spent in forums: 2 Days 12 h 38 m 44 sec
Reputation Power: 36
Need GD help - output is gibberish
I need to pull images, resize them, and stick them in place with other html. I'm experienced with php, but not GD...
I've found sample code that seems to do what I need. Almost. When the code is run by itself, it does what it should and outputs the resized image to the browser.
But it only works when the image is the only thing on the page. When I try to embed this code so that the output is in with other output, I get of gibberish data for the image instead of the image.
I suspect this has something to do with the header() bit of the code, but not sure. Please help!
-G
PHP Code:
/*
Created by: Matthew Harris
...other notes...
*/
function thumb($source, $scale, $quality = 80)
{
/* Check for the image's exisitance */
if (!file_exists($source)) {
echo 'File does not exist!';
}
else {
$size = getimagesize($source); // Get the image dimensions and mime type
$w = $size[0] / $scale; // Width divided
$h = $size[1] / $scale; // Height divided
$resize = imagecreatetruecolor($w, $h); // Create a blank image
/* Check quality option. If quality is greater than 100, return error */
if ($quality > 100) {
echo 'The maximum quality is 100. <br>Quality changes only affect JPEG images.';
}
else {
header('Content-Type: '.$size['mime']); // Set the mime type for the image
switch ($size['mime']) {
case 'image/jpeg':
$im = imagecreatefromjpeg($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG
imagejpeg($resize, '', $quality); // Output the new JPEG
break;
case 'image/png':
$im = imagecreatefrompng($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original PNG
imagepng($resize, '', $quality); // Output the new PNG
break;
}
Posts: 1,795
Time spent in forums: 2 Weeks 5 Days 15 h 23 m 11 sec
Reputation Power: 82
an image is a separate file on the webserver. so if you include image data into a page, it will look like random crap. it must be separate from teh rest of the output.
what you can do is have an external file that just resizes and call it by
<img src='resize.php?filename=image.jpg&width=100&height=100'>
if you put the resize code in resize.php it should work
Posts: 215
Time spent in forums: 2 Days 12 h 38 m 44 sec
Reputation Power: 36
Quote:
Originally Posted by replax
an image is a separate file on the webserver. so if you include image data into a page, it will look like random crap. it must be separate from teh rest of the output.
what you can do is have an external file that just resizes and call it by
<img src='resize.php?filename=image.jpg&width=100&height=100'>
if you put the resize code in resize.php it should work
Sweeet. Works like a charm. And makes sense to boot! Thanks!