Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Image Quality When Resizing
Old 02-17-2008, 05:04 PM Image Quality When Resizing
Average Talker

Posts: 27
Location: CT
Trades: 0
I found an image thumbnail script online that after modifying to what I need works fine, except the quality of the thumbnail that it creates is marginal at best.

Can someone that knows about image scripts look at this code and tell me what needs to be done to improve the quality of the thumbnails?

Code:
<?
	} else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {
	
	// Set $url To Equal The Filename For Later Use
		$url = $_FILES['imagefile']['name'];
		
	if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { 

	// Get The File Extention (.jpg, .gif or .php)
		$file_ext = strrchr($_FILES['imagefile']['name'], '.');

	// Move Image From Temporary Location To Permanent Location
		$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . strtolower($_FILES['imagefile']['name']));

	// If The Script Was Able To Copy The Image To It's Permanent Location 
			if ($copy) {

			// Successfull Upload
				$url = strtolower($url);
				$simg = imagecreatefromjpeg("$idir" . $url);

	// Make A New Temporary Image To Create The Thumbnail From 
			
			// Current Image Width
				$currwidth = imagesx($simg);

			// Current Image Height 
				$currheight = imagesy($simg);

			// If Height Is Greater Than Width 
				if ($currheight > $currwidth) {

				// Length Ratio For Width
					$zoom = $twidth / $currheight;

				// Height Is Equal To Max Height 
					$newheight = $theight;

				// Creates The New Width
		         $newwidth = $currwidth * $zoom;

			} else {
			
	// Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 

				// Length Ratio For Height 
					$zoom = $twidth / $currwidth;

				// Width Is Equal To Max Width 
					$newwidth = $twidth;

				// Creates The New Height
					$newheight = $currheight * $zoom;
			} 

	// Make New Image For Thumbnail
			$dimg = imagecreate($newwidth, $newheight);

		// Create New Color Pallete
			imagetruecolortopalette($simg, false, 256);
			$palsize = ImageColorsTotal($simg); 

		// Counting Colors In The Image
			for ($i = 0; $i < $palsize; $i++) {
			
			// Number Of Colors Used
				$colors = ImageColorsForIndex($simg, $i);

			// Tell The Server What Colors This Image Will Use
				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
      } 

		// Copy Resized Image To The New Image (So We Can Save It)
			imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); 
	    
		// Saving The Image
			imagejpeg($dimg, "$tdir" . $url);

	      imagedestroy($simg);   // Destroying The Temporary Image 
	      imagedestroy($dimg);   // Destroying The Other Temporary Image
      
		// Resize successful
			echo '<body onLoad="doLoad()"><table><tr><td><img src="http://www.carcityofdanbury.com/inventory/'.$idir.strtolower($url).'" width="365" height="274" /></td></tr><tr><td class="upTxt"><img src="../images/Common/Icons/bulletOn.gif"> Image successfully added.</td></tr><tr><td class="upTxt"><img src="../images/Common/Icons/bulletOn.gif"> Thumbnail successfully created.</td></tr></table><div class="close">'.$btmLnks.'</div>'; 
			if ($copy) {$mysql_query = "UPDATE Inventory SET $field = '$url' WHERE stock= '$stock'"; mysql_query($mysql_query) or die(mysql_error());}
		} else { 
		// If Upload Failed 
			echo '<font color="#FF0000">ERROR: Unable to upload image.</font>';
		} 
	} else { 
	// Not .jpg or .JPG file
		echo '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; 
		echo $file_ext;   // Show The Invalid File's Extention 
		echo '.</font>'; 
	} 
}

 ?>
DBookatay is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-17-2008, 05:36 PM Re: Image Quality When Resizing
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Well, the problem is not with the script (Really), it's how there being re sized (From what i can gather). For example a large picture going down to a small one is bound to lose it's quality.

You though of using a shell script?
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 02-17-2008, 07:14 PM Re: Image Quality When Resizing
Average Talker

Posts: 27
Location: CT
Trades: 0
Quote:
Originally Posted by rogem002 View Post
You though of using a shell script?
What is a shell script?
DBookatay is offline
Reply With Quote
View Public Profile
 
Old 02-17-2008, 07:44 PM Re: Image Quality When Resizing
Sharon_leic's Avatar
Super Talker

Posts: 115
Name: Sharon
Location: Leicester, uk
Trades: 0
I use a different script and works good and looks simpler than yours

add this near the top of your php

PHP Code:
function createThumbs($fname$pathToImages$pathToThumbs$thumbWidth ) {
      
$img imagecreatefromjpeg"{$pathToImages}{$fname});
      
$width imagesx$img );
      
$height imagesy$img );
      
$new_width $thumbWidth;
      
$new_height floor$height * ( $thumbWidth $width ) );
      
$tmp_img imagecreatetruecolor$new_width$new_height );
      
imagecopyresized$tmp_img$img0000$new_width$new_height$width$height );
      
imagejpeg$tmp_img"{$pathToThumbs}{$fname});
    } 

and then this line where you want to call the function
PHP Code:
createThumbs($imagename,"path to the image","thumbnailoutpath",120); 
where there is 120, change to size of the thumbnail you want it to create

this for me makes full quality thumbnails

Shaz x
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
Sharon_leic is offline
Reply With Quote
View Public Profile
 
Old 02-17-2008, 07:56 PM Re: Image Quality When Resizing
Sharon_leic's Avatar
Super Talker

Posts: 115
Name: Sharon
Location: Leicester, uk
Trades: 0
Another way if nobody knows whats wrong with your script is make the images slightly bigger than what you need then use a width tag to make it smaller when displaying it

so say you make it output thumnails at 120 wide

display it like this

echo "<img src="your_image.jpg" width="100" alt="image"> ";

and some of the quality comes back

messy but it works lol

Shaz x
__________________
mysql_connect("localhost", "brain", "sharon") or die(mysql_error());
mysql error: brain doesn't exist!
Sharon_leic is offline
Reply With Quote
View Public Profile
 
Old 02-18-2008, 02:25 AM Re: Image Quality When Resizing
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Holy sht.
1. You lose quality because you create thumbnail image with imagecreate() instead of imagecreatetruecolor().
2. You will always have performance and stability problems resizing images with php script. Use ImageMagick.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Image Quality When Resizing
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22751 seconds with 12 queries