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
https://forums.phpfreaks.com/topic/108062-resizing-images-with-gd/
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>'; 
  } 

}

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); 
?>

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); 
?>

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.