Jump to content

GD Image resize on the fly


michaelham

Recommended Posts

I know this is a newb question but I have been trying to figure it out for days and can't figure out whats wrong.

 

I am using this function to resize images .  All is get is scrambled crap.  I realize it's the raw code for the image but I can't get it to display.  I have tried header('Content-Type: image/jpeg'); at the top of my script but it doesn't help.  If i just output the image without resizing using standard html it shows up so I know the location is correct.  Any ideas?

 

$photo = imagecreatefromjpeg($photoLoc);
$resizedImage = resizeImage($photo,200,150);
imagejpeg($resizedImage);

 

function resizeImage($image,$new_width,$new_height=0) {

$old_width = imagesx($image);
$old_height= imagesy($image);

if($new_height==0) // if the height is not specified
                           //....calculate the relative height
$new_height= $new_width * $old_height / $old_width ;

$new_image= imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width,
$new_height, $old_width, $old_height);
return $new_image;

}

Link to comment
https://forums.phpfreaks.com/topic/169676-gd-image-resize-on-the-fly/
Share on other sites

I'm not sure your code is right... 'But I tested this code and it works flawlessly, try to create your function again out of this code..

<?php
// File and new size
$filename = 'test.jpg';
$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);
?>

When I use your script I get the same results.  My script needs to display in a table, about 10 pictures along with text information that corresponds to each one.  If I comment out the header() line in your script I get jumbled junk.  If I don't comment it out I get the Internall Server Error 500 because I am trying to modify the header information after things have already been written to the browser.  If I move the header() function all the way to the top of my script I get the same jumbled junk but no formatting, table, etc.  Just plain jumbled text on a white screen. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.