To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Newbies

Newbies Help for those who are just getting started

Reply
 
Thread Tools Rate Thread Display Modes
Old 02-14-2006, 02:37 PM   #1
tripodkid
Member
 
Join Date: Nov 2005
Location: Connecticut USA
Posts: 41
[RESOLVED] Help with image scaler

Hello!

This script is driving me crazy!

I'm retreving fullsize images from a db and trying to return them into a table as thumbnails.

$image_path echos the correct path to the image but $thumbnail always echos 1.

Thanks for you help!
PHP Code:
<?php

do {
// Constants
define(MAX_WIDTH, 300);
define(MAX_HEIGHT, 300);

//Get image
$image_path = $row_rs_images['image_path'];

// Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if (
$ext == 'jpg' || $ext == 'jpeg') {
    
$img = @imagecreatefromjpeg($image_path);
    } else {
        echo
"This is not a jpeg image";
        exit;
    }

// If an image was successfully loaded, test the image for size
if ($img) {

    
// Get image size and scale ratio
    
$width = imagesx($img);
    
$height = imagesy($img);
    
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

    
// If the image is larger than the max shrink it
    
if ($scale < 1) {
        
$new_width = floor($scale*$width);
        
$new_height = floor($scale*$height);

        
// Create a new temporary image
        
$tmp_img = imagecreatetruecolor($new_width, $new_height);

        
// Copy and resize old image into new image
        
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                         
$new_width, $new_height, $width, $height);
        
imagedestroy($img);
        
$img = $tmp_img;        
    }    
}

$thumbnail = imagejpeg($img, '0' ,'100');
echo
'<table width="65%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <td width="31%"><div align="center"><img src='
. $thumbnail. '/>    
    <td width="68%">&nbsp<br /><br />
    </td></td>
  </tr>
</table>
<p>&nbsp;</p>'
;
         } while (
$row_rs_images = mysql_fetch_assoc($rs_images));

?>
tripodkid is offline   Reply With Quote
Old 02-14-2006, 02:54 PM   #2
bodzan
Senior Member
 
Join Date: May 2003
Location: Novi Sad, Serbia
Posts: 576
Not quite sure, but...

You will have to ask a server to create a picture first with imagecreatetruecolor and after that to have it define a proper header as in...
PHP Code:
header ("Content-type: image/jpeg");
...before you can show the image in your browser.

I rarelly use these functions but I figure you might want to create a new file to show a picture and to call it from a page where you want that picture shown. Like:
PHP Code:
<img src="pic.php?id=1">
...where id would be an id of the thumbnail you want to show...
__________________
I know that I don't know nothing...
bodzan is offline   Reply With Quote
Old 02-14-2006, 03:00 PM   #3
codefetch
Junior Member
 
Join Date: Feb 2006
Location: Portland, Oregon
Posts: 3
confusing image path with image data

If you want to display images using <img src='. $thumbnail. '/> you'll have to write-out the thumbnails to disk somewhere where your web server can serve them from.

I did a codefetch search for image thumbnail and found this example of uploading images and creating thumbnails from the book Beginning PHP5, Apache, and MySQL Web Development. The text is hot-linked to PHP documentation so you can click on PHP commands in the code to see the syntax. See for example how they use imagejpeg.

Last edited by codefetch; 02-14-2006 at 03:04 PM. Reason: forgot to credit the book
codefetch is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 11:33 AM.






Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.