LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2005, 06:08 AM   #1
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Rep: Reputation: 30
Images in PHP


Hi. I need to know how to resize an image in a PHP script. It only has to go smaller. Unfortunately, I can't seem to find anythinga bout it either in my PHP book or in the online documentation.

A function that does it for you would be great, but I'm prepared to code it myself, if somebody would just tell me where to start...

Im thinking "In the case of an image that's 3 times too big, read every third line of that image and put it into a new one. Then read every third column of that image, and put it into the final one."

So you see it doesn't have to be pretty (although pretty would be nice) - it just has to work.

Any ideas?


[edit]
I guess to make it pretty, I'd have to say "look at every block of nine pixels, take the average colour, and write that as a new pixel in the final image"... but I don't know how to do that either

Last edited by Napalm Llama; 04-05-2005 at 06:10 AM.
 
Old 04-05-2005, 06:27 AM   #2
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
You need the GD2 libraries enabled. I can give you a sample code when I get home. Searched for it for quite some time.
 
Old 04-05-2005, 06:36 AM   #3
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
I think GD is there.

Looks like there actually is a function to do it in PHP, though, and it looks like this:
imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )

Well, looks like I'm going to have fun working that one out
 
Old 04-05-2005, 08:41 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Here's a thumbnailing function I wrote in PHP quite awhile ago. Should give you a good start, though. This particular one resizes it so that the width is always 100, and the height of the thumbnail is the same approximate ratio as the original.

PHP Code:
function MakeThumb($src$dest)
{
    
$img imagecreatefromjpeg($src);

    if (
$img) {

        
$imginfo getimagesize($src);
        
$w $imginfo[0];
        
$h $imginfo[1];

        
$w1 100;
        
$h1 $w1 $h $w;
        
$thumb imagecreate($w1$h1);

        if (
ImageCopyResized($thumb$img0000$w1$h1$w$h)) {
            if (
ImageJPEG($thumb$dest)) {
                return 
1;
            }
            else {
                return 
0;
            }
        }
        else {
            return 
0;
        }
    }
    else {
        return 
0;
    }

 
Old 04-05-2005, 11:01 AM   #5
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
Woo! Thanks

Hmm, I should have said this a couple of hours ago...
I've actually figured out how to do it myself

PHP Code:
<?php

$xlim 
300;    // Maximum width
$ylim 150;    // Maximum height
$hi_q false;    // Do resampling?

$file $_REQUEST["file"];
include(
"functions.php");

// Check whether it really is an image
if( !isimg($file) ) {
    die(
"bad image");
}

// Decide which image type we're working with
switch( isimg($file) ) {
    case 
"jpg":
        
$orig imagecreatefromjpeg$file );
        
header('Content-type: image/jpeg');
        
imagejpeg(resize($orig));
        break;
    
    case 
"jpeg":
        
$orig imagecreatefromjpeg$file );
        
header('Content-type: image/jpeg');
        
imagejpeg(resize($orig));
        break;
    
    case 
"gif":
        
$orig imagecreatefromgif$file );
        
header('Content-type: image/gif');
        
imagegif(resize($orig));
        break;
    
    case 
"png":
        
$orig imagecreatefrompng$file );
        
header('Content-type: image/png');
        
imagepng(resize($orig));
        break;
}

// Does all the non image-type specific bits
function resize$imgin ) {
    
    
// Import globals
    
global $xlim$ylim$hi_q;
    
    
// Get the current sizes
    
$ox imagesx$imgin );
    
$oy imagesy$imgin );
    
    
// Get the percentage change from old to potential new
    
$xdiff $xlim / ( $ox/100 );
    
$ydiff $ylim / ( $oy/100 );
    
    
// If new image is larger than old, just use old
    
if( $xdiff 100 && $ydiff 100 ) {
        return 
$imgin;
    }
    
    
// Decide which dimension is the most oversized, and calculate ratio from that
    
if( $xdiff $ydiff ) {
        
$ratio $ydiff/100;
    } else {
        
$ratio $xdiff/100;
    }
    
    
// Calculate the new dimensions
    
$nx $ox $ratio;
    
$ny $oy $ratio;
    
    
// Make a new canvas for the output image
    
$imgout imagecreatetruecolor$nx$ny );
    
    
// Shrink old image and copy to new canvas
    
if( $hi_q ) {
        
imagecopyresampled($imgout$imgin0000$nx$ny$ox$oy);
    } else {
        
imagecopyresized($imgout$imgin0000$nx$ny$ox$oy);
    }
    
    
// Send shrunken image back to the caller
    
return $imgout;
    
}

?>
All you need in addition is a functions.php containing this:
PHP Code:
function isimg($name) {
    
$file_exts explode(".",$name);
    
$ext count($file_exts) - 1;
    
$ext strtolower($file_exts[$ext]);
    if ( 
$ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png" ) {
        return 
$ext;
    } else {
        return 
false;
    }

And you're set. You use it like this:
Code:
<img src="img.php?file=bigpicture.jpg" alt="Thumbnail of BigPicture">

What do you think?
I'm working on a caching thingy at the moment, so it doesn't hog so much CPU time.

It's being used as part of the project described in my sig, which is why it has to refer out to the functions.php file.
At the moment all the application does as a whole is let you browse your file tree remotely, and execute shell commands as 'nobody'.
I'm going to add caching though, because if I'm browsing my pictures directory, all those thumbnails take quite a while to generate...

Last edited by Napalm Llama; 04-05-2005 at 11:02 AM.
 
Old 04-06-2005, 08:17 AM   #6
Napalm Llama
Member
 
Registered: Nov 2004
Location: Bristol, UK
Distribution: Gentoo 2005.0
Posts: 224

Original Poster
Rep: Reputation: 30
Yay! A release!

If anybody passed by yesterday and was interested in this, they can now take it for a test drive...

www.yellowmackerel.co.uk/os/phpfinder.htm
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & GD Library - Combine Two Images wh33t Programming 1 11-29-2005 09:12 AM
php upload an array of images spoody_goon Programming 3 05-22-2005 11:42 AM
PHP script to check filetypes and put images into SQL table benrose111488 Programming 5 03-02-2005 01:57 AM
PHP and MySQL (Images) Jayantha Linux - Newbie 1 05-20-2004 03:45 AM
changing images in php devit Programming 0 04-04-2004 11:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration