Jump to content

resizing images with gd


garry

Recommended Posts

So my users are able to upload a picture when they create a new artist on my site. I don't have any problems storing the picture, I'm only having troubles resizing it to 300x300. Here's the code I'm using:

 

$artistart = $HTTP_POST_FILES['image'];

	if ($artistart['type'] == 'image/jpeg') {

	$tempimg = $artistart['tmp_name'];

	$tempsize = getimagesize($tempimg);

	$width = $tempsize['0'];
	$height = $tempsize['1'];

	$newwidth = 300;
	$newheight = 300;

	$uniq = uniqid("");

	$filename = DOCUMENT_ROOT . 'artistart/'.$uniq . '.jpg';

	$thumb = imagecreatetruecolor($newwidth, $newheight); 

	$source = imagecreatefromjpeg($tempimg); 

        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

	return imagejpeg($thumb,$filename); 

	}

 

When I do this, I get a bunch of very odd (and long) code appear across the screen instead of the image I want. Can anyone help??

 

Thanks in advance!

Link to comment
Share on other sites

Pretty sure this works. Let me know if it doesnt.

<?php
$idir = "../media/photos/";   // Path To Images Directory 
$tdir = "../media/photos/thumbs/";   // Path To Thumbnails Directory 
$twidth = "125";   // Maximum Width For Thumbnail Images 
$theight = "100";   // Maximum Height For Thumbnail Images 
$mwidth = "600";   // Maximum Width For main Images 
$mheight = "480";   // Maximum Height For main Images 
if (!isset($_GET['subpage'])) { ?> 
<h3>Upload Photo</h3>
  <form method="post" action="?subpage=upload" enctype="multipart/form-data"> 
   File:<br /> 
  <input type="file" name="imagefile" class="form"> 
  <br /><br /> 
  <input type="text" name="photoname">
  <br><br>
  <textarea name="description"></textarea><br><br>
  <input name="submit" type="submit" value="Sumbit" class="form">  <input type="reset" value="Clear" class="form"> 
  </form> 
<? 
} 
else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { 
$url = $_FILES['imagefile']['name'];
		if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") 
	{ 
    		$file_ext = strrchr($_FILES['imagefile']['name'], '.');
   		    $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);
    			if ($copy) {
$simg = imagecreatefromjpeg("$idir" . $url);
      $currwidth = imagesx($simg);   // Current Image Width 
      $currheight = imagesy($simg);   // Current Image Height 
      if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
         $zoom = $mwidth / $currheight;   // Length Ratio For Width 
         $newheight = $mheight;   // Height Is Equal To Max Height 
         $newwidth = $currwidth * $zoom;   // Creates The New Width 
      } else {
        $zoom = $mwidth / $currwidth;   // Length Ratio For Height 
        $newwidth = $mwidth;   // Width Is Equal To Max Width 
        $newheight = $currheight * $zoom;   // Creates The New Height 
      } 
      $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
      $palsize = ImageColorsTotal($simg); 
      for ($i = 0; $i < $palsize; $i++) {
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
      } 
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);
      imagejpeg($dimg, "$idir" . $url);

      print 'Image uploaded successfully.<br />'; 
  
      $simg = imagecreatefromjpeg("$idir" . $url);
      $currwidth = imagesx($simg);   // Current Image Width 
      $currheight = imagesy($simg);   // Current Image Height 
      if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
         $zoom = $twidth / $currheight;   // Length Ratio For Width 
         $newheight = $theight;   // Height Is Equal To Max Height 
         $newwidth = $currwidth * $zoom;   // Creates The New Width 
      } else {
        $zoom = $twidth / $currwidth;   // Length Ratio For Height 
        $newwidth = $twidth;   // Width Is Equal To Max Width 
        $newheight = $currheight * $zoom;   // Creates The New Height 
      } 
      $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
      imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
      $palsize = ImageColorsTotal($simg); 
      for ($i = 0; $i < $palsize; $i++) {
       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
      } 
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);
      imagejpeg($dimg, "$tdir" . $url);
      imagedestroy($simg);   // Destroying The Temporary Image 
      imagedestroy($dimg);   // Destroying The Other Temporary Image 
      print 'Image thumbnail created successfully.';   // Resize successful 
    } else { 
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed 
    } 
  } else { 
    print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';
    print $file_ext;   // Show The Invalid File's Extention 
    print '.</font>'; 
  } 

}

Link to comment
Share on other sites

Thanks, this is the sort of thing I'm looking for! One problem though - the resized image and the thumbnail (thumb especially) are reduced to quite low quality after running this script. I want to have good quality images for my site! How can I improve the quality?

Link to comment
Share on other sites

Okay, so now the quality is fine, but the colors are all messed up after using that script. It's mainly black and white now. Here's the code I'm using, I took some of it from Gighalens script:

 

<?php
$artistart = $HTTP_POST_FILES['image'];

	if ($artistart['type'] == 'image/jpeg') {

	$tempimg = $artistart['tmp_name'];

	$tempsize = getimagesize($tempimg);

	$width = $tempsize['0'];
	$height = $tempsize['1'];

	$uniq = uniqid("");

	$filename = DOCUMENT_ROOT . 'artistart/'.$uniq . '.jpg';

	$simg = imagecreatefromjpeg($tempimg);

	$newwidth = 300;
	$newheight = 300;

	$newimage = imagecreate(300, 300);

	$palsize = imagecolorstotal($simg);

	for ($i = 0; $i < $palsize; $i++) {

       $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 

       imagecolorallocate($newimage, $colors['red'], $colors['green'], $colors['blue']);

   		  } 

	$thumb = imagecreatetruecolor($newwidth, $newheight); 

	$source = imagecreatefromjpeg($tempimg); 

    imagecopyresampled($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	imagejpeg($newimage,$filename,100); 
?>

Link to comment
Share on other sites

Okay, so now the quality is fine, but the colors are all messed up after using that script. It's mainly black and white now. Here's the code I'm using, I took some of it from Gighalens script:

 

<?php
$artistart = $HTTP_POST_FILES['image'];

	if ($artistart['type'] == 'image/jpeg') {

	$tempimg = $artistart['tmp_name'];

	$tempsize = getimagesize($tempimg);

	$width = $tempsize['0'];
	$height = $tempsize['1'];

	$uniq = uniqid("");

	$filename = DOCUMENT_ROOT . 'artistart/'.$uniq . '.jpg';

	$simg = imagecreatefromjpeg($tempimg);

	$newwidth = 300;
	$newheight = 300;

	$newimage = imagecreate(300, 300);

	$palsize = imagecolorstotal($simg);

	for ($i = 0; $i < $palsize; $i++) {

        $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 

        imagecolorallocate($newimage, $colors['red'], $colors['green'], $colors['blue']);

    		  } 

	$thumb = imagecreatetruecolor($newwidth, $newheight); 

	$source = imagecreatefromjpeg($tempimg); 

    imagecopyresampled($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	imagejpeg($newimage,$filename,100); 
?>

 

Give this a try

 

<?php
    $artistart = $HTTP_POST_FILES['image'];
    
        if ($artistart['type'] == 'image/jpeg') {
        
        $tempimg = $artistart['tmp_name'];
        
        $tempsize = getimagesize($tempimg);
        
        $width = $tempsize['0'];
        $height = $tempsize['1'];
        
        $uniq = uniqid("");
        
        $filename = DOCUMENT_ROOT . 'artistart/'.$uniq . '.jpg';
        
        $simg = imagecreatefromjpeg($tempimg);
        
        $newwidth = 300;
        $newheight = 300;

        $newimage = imagecreatetruecolor($newwidth, $newheight); 

        imagecolortransparent($newimage, imagecolorallocate($newimage, 0, 0, 0));
        imagecopyresampled($newimage, $simg, 0, 0, 0, 0, $newwidth, $newheight, imagesx($src), imagesy($src));        
        
        imagejpeg($newimage, $filename, 100); 
?>

Link to comment
Share on other sites

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.