Jump to content

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.