Jump to content

Recommended Posts

I'm working on a registration form and you must upload a photo. Then the photo the person uploads must be copied and resized to 60x60... the upload part is working, it's the resize part that is acting up...

 

this is the code I'm using

<?php

function resizeImage($image)
{

	$imgSize = getimagesize($image);
	$imgWidth = $imgSize[0];
	$imgHeight = $imgSize[1];
	$newWidth = "60";
	$newHeight = "60";

	$source = imagecreatefromjpeg($image);
	$thumb = imagecreatetruecolor($newWidth, $newHeight);

	imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
	imagejpeg($thumb);

}

?>

 

any ideas what I'm doing wrong?... this is the error i get

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC $.' ",#(7),01444'

 

=82<.342ÿÛC 2!!

ÿÀ<<"ÿÄ ÿĵ

 

there's alot more weird symbols in the error message, but that's all the was stored when I copy/paste it...

 

Thanks In Advance

- Clown

Link to comment
https://forums.phpfreaks.com/topic/51873-solved-help-image-resize/
Share on other sites

<?php

 

function resizeImage($image)

{

 

$imgSize = getimagesize($image);

$imgWidth = $imgSize[0];

$imgHeight = $imgSize[1];

$newWidth = "60";

$newHeight = "60";

 

$source = imagecreatefromjpeg($image);

$thumb = imagecreatetruecolor($newWidth, $newHeight);

 

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);

header('Content-type: image/jpeg'); // <-- you need that!

imagejpeg($thumb);

 

}

 

?>

Here is the code that I normally use to resize an image with good quality, plus JPEG optimization:

 

          // create thumbnail image, resized and optimized

          $destimg=ImageCreateTrueColor(60,60) or die("Problem In Creating image");

          $srcimg=ImageCreateFromJPEG($binFile) or die("Problem In opening Source Image");

          ImageCopyResampled($destimg,$srcimg,0,0,0,0,60,60,ImageSX($srcimg),ImageSY($srcimg)) or die("Problem In resizing");

          ob_start();

          imagejpeg($destimg, '', 35);

          $thumb_data = addslashes(ob_get_contents());

          ob_end_clean();

          imagedestroy($destimg);
          imagedestroy($srcimg);

 

After that, your $thumb_data var would have the binary data for the image and you can either save it into a database or write it to a file using fwrite().. The above code was intended to save into a database, so there might be an easier way to save directly to a file..

 

hmm... so what you're saying is that... I should have a table named something like "news_userimg" and store all info to that?

 

another question: how is the best way to scale the image so it looks good? cuz resizing a 250x100 to 60x60 will look really bad...

<?php

   function resizeImage($image)
   {
   
      $imgSize = getimagesize($image);
      $imgWidth = $imgSize[0];
      $imgHeight = $imgSize[1];
      $newWidth = "60";
      $newHeight = "60";
      
      $source = imagecreatefromjpeg($image);
      $thumb = imagecreatetruecolor($newWidth, $newHeight);
      
      imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
      $target = './thumbnails/'.$imagename;
      return imagejpeg($thumb, $target); // this is how you save an picture   
   }

?>

Deciding to store images in a database all depends on the situation.. If you do decide to, then yes, you should make an images table with fields such as id, filename, data, modified .. Make sure that the data field is binary..

 

As for getting the highest quality resize possible, please see my previous post that uses imagecopyresampled() instead of imagecopyresized()

 

ok.. let me see if i got it...

 

use imagecopyresampled() not imagecopyresized()

 

as for the database

Fields:

- id

- filename

- data <-- this has to be binary? what do I store here?

- modified <-- what do I store here?

 

should I also add these fields?

- width

- height

The 'filename' and 'modified' fields are optional, but they would typically be used to keep track of a filename to represent the image and the time that the image was last modified (this would be a TIMESTAMP field).. And yes, the 'data' field must be binary in order to store the image data..

 

I'm not sure why you would need to store the whole filepath, since the image will be in the database and there is no physical disk path for it..

 

As for height and width, you could store them to make it easier to generate afterwards instead of detecting the height and width each time, but if you are already resizing to a specific image size, then you probably already know the size and can just hard code it on the output part of the code..

 

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.