Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old September 16th, 2002, 10:15 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Article Discussion: Resizing JPEG's With PHP and GD

If you have any questions or comments about this article then please post them here.

This forum post relates to this article

Reply With Quote
  #2  
Old May 22nd, 2003, 02:23 PM
jben.net jben.net is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 51 jben.net User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 41 sec
Reputation Power: 6
Send a message via AIM to jben.net
turning script on it's head...

Hi,

Thanks for writing this tutorial, it's really helped me loads ;-)

What I'm trying to do is set a max width and height for the main pic (with ratio) and save that and then make a set width / height thumbnail without ratio.

I've got it all working fine, except 1 thing. The quality of the images once they've been resized is awful, is there any way of making this better ???

// here's my code...
PHP Code:
<?php

if($_POST['upload'] == 'now')
{
            
            
$report_img '';

            
$MAX_IMG_WIDTH 700;       
            
$MAX_IMG_HEIGHT 500;
            
$THUMB_WIDTH 40;
            
$THUMB_HEIGHT 40;
            
$IMG_BIG "./big";
            
$IMG_THUMB "./thumbs";
            
            
$fileName explode("."$_FILES['strFile']['name']);
            
$name str_replace(' ''_'$fileName[0]);
            
            
error_reporting(53); //errors may happen for no reason, so this killes them, we will use our own error reporting
            
$acceptedTypes = array('image/jpeg','image/jpg'); // 
        
            
if(!in_array($_FILES['strFile']['type'], $acceptedTypes) || trim($_FILES['strFile']['tmp_name']) == "" || trim($_FILES['strFile']['tmp_name']) =="none")
                    {
                    
                            echo 
'You didnt suply an image a valid image.';
                    
                    }
                else
                    {
                    
                            
                            
$img_orig_size getimagesize($_FILES['strFile']['tmp_name']);
                            
$img_orig_width $img_orig_size[0];
                            
$img_orig_height $img_orig_size[1];
                            
                                                                                        
                            
                            
//$img_original = ImageCreateFromJpeg($_FILES['strFile']['tmp_name']);

                            
if($img_orig_width >= $MAX_IMG_WIDTH || $img_orig_height >= $AX_IMG_HEIGHT)
                            {
                                
$mlt_w $MAX_IMG_WIDTH $img_orig_width;
                                
$mlt_h $MAX_IMG_HEIGHT $img_orig_height;
                                
$mlt $mlt_w $mlt_h $mlt_w:$mlt_h;
                                                
                                
#### Calculate new dimensions
                                
$img_new_width =  round($img_orig_width $mlt);
                                
$img_new_height =  round($img_orig_height $mlt);
                                                
                                
$img_resized ImageCreate($img_new_width$img_new_height);

                                
imagecopyresized($img_resizedImageCreateFromJpeg($_FILES['strFile']['tmp_name']), 0$img_new_width$img_new_height$img_orig_width$img_orig_height);

                                
$time time();
                                
$timeSub substr($time44);
                                
$image_stored $timeSub '_' $_POST['strTitle']  . "_$name.jpg";
                                
ImageJPEG($img_resized"$IMG_BIG/$image_stored");

                                
ImageDestroy($img_resized);
                            }
                            else
                            {
                                
$time time();
                                
$timeSub substr($time44);
                                
$image_stored $timeSub '_' $_POST['strTitle']  . "_$name.jpg";
                                
ImageJPEG($img_original"$IMG_BIG/$image_stored");
                            }
                            
                            echo 
"Image Name: {$_FILES['strFile']['name']}<br>
                                       New Image Name: $image_stored<br>
                                       Width: $img_orig_width<br>
                                       Height: $img_orig_height<br>
                                       Max Image Size for Thumbnails: $IMG_WIDTH x $IMG_HEIGHT<br>    
                                       <a target='_blank' href='$IMG_BIG/$image_stored'>View Original Image</a><br><br>"
;
                            
                            if(
$img_orig_width <= $THUMB_WIDTH || $img_orig_height <= $THUMB_HEIGHT)
                                    {
                                            echo 
"<b>The image that you selected didnt need to be resized</b>.<br>You may want to upload a bigger picture.";
                                    }
                                else
                                    {
                                            
//the image will need to be resized, as its too big. It will be a thumbnail
                                            
                                            /*$mlt_w = $IMG_WIDTH / $img_orig_width;
                                            $mlt_h = $IMG_HEIGHT / $img_orig_height;
                                            $mlt = $mlt_w < $mlt_h ? $mlt_w:$mlt_h;
                                                
                                            #### Calculate new dimensions
                                            $img_new_width =  round($img_orig_width * $mlt);
                                            $img_new_height =  round($img_orig_height * $mlt);*/
                                                
                                            
$img_resized ImageCreate($THUMB_WIDTH$THUMB_HEIGHT);
                                                
                                            
imagecopyresized($img_resizedImageCreateFromJpeg($_FILES['strFile']['tmp_name']), 0$THUMB_WIDTH$THUMB_HEIGHT$img_orig_width$img_orig_height);                                                                                                                                                                                                                                    
                                                
                                            
$img_name "tn_$image_stored";
                                                
                                            
Imagejpeg($img_resized"$IMG_THUMB/$img_name");
                                                
                                            
ImageDestroy($img_resized);
                                            
                                            
$mg_new_size filesize("$IMG_THUMB/$img_name");
                                            
                                            echo 
"<b>Image Resized</b><br>
                                                        Width: $img_new_width<br>
                                                        Height: $img_new_height<br>
                                                        FileSize: $mg_new_size (in bytes)<br>
                                                        <img src='$IMG_THUMB/$img_name'>"
;
                                            
                                            
                                    }
                            
                    
                    }
}
else
{
?>


Thanks in advance,

Jon

Reply With Quote
  #3  
Old May 25th, 2003, 10:12 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Jon,

If I'm reading your code correctly, you're trying to resize all your images to 700x500. The problem with this is that not all of your images will resize to that proportionally...

What you need to do is calculate the best conversion scale (aspect ratio) to maintain the image quality:

Code:
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

if ($scale < 1){
      $new_width = floor($scale * $width);
      $new_height = floor($scale * $height);


Once you receive your new width and height, you use those new values to resize your image...

HTH!
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > Article Discussion: Resizing JPEG's With PHP and GD


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway