Question

resize into thumbnail

Asked by: lobangkin


I really need a simple script that will auto resize my image to a thumbnail, can someone write me one?


let say I have an image width 100 height 200, I need the script to shrink it down say 100%?

becoming width 50 and height 100, possible? Just make it into thumbnail at what every size they have.

Any help appreciated, thank you!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-08-31 at 09:35:58ID20725035
Topic

PHP Scripting Language

Participating Experts
5
Points
415
Comments
31

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. creating a thumbnail
    How can create a thumbnail from a given bmp/jpg/any format (with minimum quality lose!)
  2. Thumbnails
    I'm trying to create thumbnails of images in a directory. The TImage can also read Gif files (I have RxGif in library). I have managed to get this to work but a couple of problems are bugging me. (I'm using D3 Pro). 1) When I try thumbnails for a directory with a large numbe...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: shmertPosted on 2003-08-31 at 10:03:26ID: 9257473

Seems like an imageResize class would be good, where you specify the maxHeight, maxWidth, minHeight, minWidth, or zoom.  Also the conversion method to use, where method is one of imagemagick, gd, or whatever.  Then specify the output format, and feed in the source image.  And also maybe have a file cache for already converted images, so if the parameters all match up with an existing image it gets used instead of re-resizing it.  Now, who wants to write it?  :)

 

by: lobangkinPosted on 2003-08-31 at 10:08:43ID: 9257486

shmert can you help? I can increase another 100 points.

 

by: lobangkinPosted on 2003-08-31 at 10:12:18ID: 9257492

this is the image source I get from my database

<img src='".$row[image]."' width='100'  height='200'>

 

by: lobangkinPosted on 2003-08-31 at 10:25:16ID: 9257541

someone Help please.

 

by: lobangkinPosted on 2003-08-31 at 10:25:42ID: 9257542

..

 

by: lobangkinPosted on 2003-08-31 at 11:53:39ID: 9257883

I really need a simple script that will auto resize my image to a thumbnail, can someone write me one?
let say I have an image width 100 height 200, I need the script to shrink it down say 100%?

As long as the picture don't stretch or skew...

 

by: chofreitherPosted on 2003-08-31 at 13:46:05ID: 9258268

No need to reinvent the wheel. Use this: http://mightystuff.net/thumbnail_script

Clemens

 

by: Mark_KendallPosted on 2003-08-31 at 14:36:59ID: 9258415

do you have gd available? If you do this is a nice class......


<?php

/**
*      class Image2Thumbnail
*   Thumbnail creation with PHP4
*
*
*   @author     Andreas Martens <heyn@plautdietsch.de>
*      @version      0.9
*/

class Img2Thumb      {

/**
*   Constructor - requires following vars:
*
*      @param string $filename                  image path
*
*      These are additional vars:
*
*      @param int $newxsize                  new maximum image width
*      @param int $newysize                  new maximum image height
*      @param string $fileout                  output image path
*
*/
      function Img2Thumb($filename, $newxsize=60,$newysize=60, $fileout='')      {
            global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_COOKIE_VARS;

            if (isset($HTTP_COOKIE_VARS)) $httpvars = $HTTP_COOKIE_VARS;
            else if (isset($HTTP_POST_VARS)) $httpvars =  $HTTP_POST_VARS;
               else if (isset($HTTP_GET_VARS)) $httpvars =  $HTTP_GET_VARS;

            $this -> NewImgCreate($filename,$newxsize,$newysize,$fileout);

      }

/**
*
*      private function - do not call
*
*/
      function NewImgCreate($filename,$newxsize,$newysize,$fileout)      {
            $type = $this->GetImgType($filename);

            if ($type=="png")      {
                   $orig_img = imagecreatefrompng($filename);
            }
            if ($type=="jpg")      {
                   $orig_img = imagecreatefromjpeg($filename);
            }

            $new_img =$this->NewImgResize($orig_img,$newxsize,$newysize,$filename);

            if (!empty($fileout))      {
                   $this-> NewImgSave($new_img,$fileout,$type);
            }
            else      {
                   $this->NewImgShow($new_img,$type);
            }

            ImageDestroy($new_img);
            ImageDestroy($orig_img);
      }

      /**
*
*      private function - do not call
*
*/
      function NewImgResize($orig_img,$newxsize,$newysize,$filename)      {

            $orig_size = getimagesize($filename);

            if ($orig_size[0]<$orig_size[1]){
                  $newxsize = $newysize * ($orig_size[0]/$orig_size[1]);
            }
            else{
                  $newysize = $newxsize / ($orig_size[0]/$orig_size[1]);
            }

            $im_out = ImageCreate($newxsize,$newysize);
            ImageCopyResized($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize, $orig_size[0], $orig_size[1]);
            return $im_out;
      }

      /**
*
*      private function - do not call
*
*/
      function NewImgSave($new_img,$fileout,$type)      {
            if ($type=="png")      {
                  if (substr($fileout,strlen($fileout)-4,4)!=".png")
                        $fileout .= ".png";
                   return imagepng($new_img,$fileout);
            }
            if ($type=="jpg")      {
                  if (substr($fileout,strlen($fileout)-4,4)!=".jpg")
                        $fileout .= ".jpg";
                   return imagejpeg($new_img,$fileout);
            }
      }

      /**
*
*      private function - do not call
*
*/
      function NewImgShow($new_img,$type)      {
            if ($type=="png")      {
                  header ("Content-type: image/png");
                   return imagepng($new_img);
            }
            if ($type=="jpg")      {
                  header ("Content-type: image/jpeg");
                   return imagejpeg($new_img);
            }
      }

      /**
*
*      private function - do not call
*
*/
      function GetImgType($filename)      {
            $size = getimagesize($filename);
            if($size[2]==2) return "jpg";
            elseif($size[2]==3) return "png";

      }

}

?>


Then put this script in the directory of images you want to thumbnail.......

<?php

// showthumb.php

//      Image2Thumbnail - class
include('/path/to/your/classes/thumbnailclass.php');

//      create new object
$neu = new Img2Thumb($filename,$newxsize,$newysize,$fileout);


?>

Then call the thumbnail like so.......

<img src="/images/full_size/showthumb.php?filename='.$original_image.'&newxsize=50&newysize=50">

Works well for me

Using.........

GD Version 1.6.2 or higher

 

by: Mark_KendallPosted on 2003-08-31 at 14:38:29ID: 9258419

P.S. The script above originally came from http://www.phpclasses.org/ Great site for finding classes to do pretty much anything.

 

by: philleePosted on 2003-08-31 at 19:41:43ID: 9259198

You can use imagemagik from: http://www.imagemagick.org/

You can download/install it, then run exec ("convert yourfile -resize widthxheight")

ImageMagik has a lot of other nifty features too.

 

by: philleePosted on 2003-08-31 at 19:41:45ID: 9259199

You can use imagemagik from: http://www.imagemagick.org/

You can download/install it, then run exec ("convert yourfile -resize widthxheight")

ImageMagik has a lot of other nifty features too.

 

by: lobangkinPosted on 2003-08-31 at 21:57:59ID: 9259621

Mark_Kendall ,

I do not know how to apply to my code..

others,

same as above..

 

by: shmertPosted on 2003-08-31 at 22:17:39ID: 9259669

Hot damn, this works!
Use a scale of .5 to shrink the image to half its original size.  50%.  To shrink an image by 100%, just delete it. :-)

function resizeImage($from, $to, $scale=0.5) {
        $src = imagecreatefromjpeg($from);
        list($x, $y) = getimagesize($from);
        $out = imagecreatetruecolor($x * $scale, $y * $scale);
        imagecopyresampled($out, $src, 0, 0, 0, 0, $x * $scale, $y * $scale, $x, $y);
        imagejpeg($out, $to);
}
 
resizeImage('Lucy.jpg', 'Lucy_small.jpg', .5);

 

by: shmertPosted on 2003-08-31 at 22:20:03ID: 9259674

Oops, forgot the cleanup code, you should probably call imagedestroy() in there to free up memory for very large images:

function resizeImage($from, $to, $scale=0.5) {
        $src = imagecreatefromjpeg($from);
        list($x, $y) = getimagesize($from);
        $out = imagecreatetruecolor($x * $scale, $y * $scale);
        imagecopyresampled($out, $src, 0, 0, 0, 0, $x * $scale, $y * $scale, $x, $y);
        imagejpeg($out, $to);
        imagedestroy($from);
        imagedestroy($out);
}
 

 

by: lobangkinPosted on 2003-08-31 at 22:33:17ID: 9259703

Hi shmert,


what is $from?
What is $to?

how to call out the image?


thanks alot

 

by: shmertPosted on 2003-08-31 at 23:05:54ID: 9259795

$from = '/path/to/the/src/image'; // image to be resized
$to = '/path/to/target/image'; // path where the image will be written

so I might call resizeImage('/uploads/bigImage.jpg', '/usr/local/apache/htdocs/thumbnails/bigImage_thumb.php');

this will write the bigImage_thumb.php to my apache folder in the "thumbnails" directory.  Then I just put this on my HTML page:

<img src="/thumbnails/bigImage_thumb.php">

NOTE:  the thumbnails directory needs to be writable by PHP!

If you don't want to write the thumbnail to a file, but just want to display it in the browser, do something like this:

<?php
#thumb.php
header("Content-type: image/jpg");
$from = '/path/to/big_image.jpg'; // maybe this could be a parameter passed in the URL
$src = imagecreatefromjpeg($from);
if (!$src) die("Unable to locate src image at $src);
list($x, $y) = getimagesize($from);
$out = imagecreatetruecolor($x * $scale, $y * $scale);
imagecopyresampled($out, $src, 0, 0, 0, 0, $x * $scale, $y * $scale, $x, $y);
imagejpeg($out);
imagedestroy($from);
imagedestroy($out);
?>

 

by: lobangkinPosted on 2003-08-31 at 23:54:06ID: 9259981

YEs I just want to display it to a browser but I got errors..

>>>>>>>>>>
Warning: imagecreatetruecolor(): Invalid image dimensions in /home/sample/public_html/sample.php on line 25
<<<<<<<<<<<<<<


my $from = 'http://www.domain.com/pics/sample.jpg';


anything wrong?

<?php
#thumb.php
header("Content-type: image/jpg");
$from = ''http://www.domain.com/pics/sample.jpg'; // maybe this could be a parameter passed in the URL
$src = imagecreatefromjpeg($from);
if (!$src) die("Unable to locate src image at $src);
list($x, $y) = getimagesize($from);
$out = imagecreatetruecolor($x * $scale, $y * $scale);
imagecopyresampled($out, $src, 0, 0, 0, 0, $x * $scale, $y * $scale, $x, $y);
imagejpeg($out);
imagedestroy($from);
imagedestroy($out);
?>

 

by: shmertPosted on 2003-09-01 at 09:18:53ID: 9262375

Yes, the $from needs to be a local file, to get the dimensions.

 

by: lobangkinPosted on 2003-09-01 at 10:51:55ID: 9262741

I input the correct path to my image but it still show that error..

 

by: shmertPosted on 2003-09-01 at 11:09:30ID: 9262829

Sorry, I forgot to define $scale = 0.5;
This sets the picture to be 1/2 size.
In the function I posted earlier, $scale is one of the function arguments.
Try this!

<?php
#thumb.php
header("Content-type: image/jpg");
$scale = 0.5;
$from = ''/pics/sample.jpg'; // maybe this could be a parameter passed in the URL
$src = imagecreatefromjpeg($from);
if (!$src) die("Unable to locate src image at $src);
list($x, $y) = getimagesize($from);
$out = imagecreatetruecolor($x * $scale, $y * $scale);
imagecopyresampled($out, $src, 0, 0, 0, 0, $x * $scale, $y * $scale, $x, $y);
imagejpeg($out);
imagedestroy($from);
imagedestroy($out);
?>            

 

by: lobangkinPosted on 2003-09-01 at 11:28:08ID: 9262903

I tried it but it displayed garbage text.

Why is it so?

 

by: shmertPosted on 2003-09-01 at 16:50:05ID: 9264009

header("Content-type: image/jpeg");

try that instead of jpg

 

by: lobangkinPosted on 2003-09-02 at 02:34:44ID: 9265942

hmm.. still the same received garbage.

 

by: shmertPosted on 2003-09-02 at 08:08:11ID: 9267788

Do you see any error messages in with the garbage?  There can't be any whitespace outside the <?php ... ?> tags, or the header won't work.   The header("Content-type: image/jpeg"); needs to be the very first thing that gets output.  This PHP script is essentially telling the browser it's an image.  The garbage is the image data.  Anything besides the garbage will be interpreted as a corrupt image, and won't be displayed properly.

 

by: lobangkinPosted on 2003-09-02 at 11:41:09ID: 9269168

yes, no whitespace outside the tags.

the error message in the garbage readable is "CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality"

 

by: shmertPosted on 2003-09-02 at 11:55:05ID: 9269282

Make sure you have the line
header("Content-type: image/jpeg");
in there.

If you see garbage and text, then the header() function isn't working right.  You should either see your resized image, or a broken image icon.  Maybe a blank screen.  But definitely not garbage.

To test this out, make a test page like this:

<?php
header("Content-type: image/jpeg");
readfile("/path/to/image.jpg");
?>

it should be an image.  If you take off the header(), you'll see similar garbage text.

Go ahead and paste the contents of your script in here, I'll take a look at it.

 

by: DarkshadeauPosted on 2003-09-03 at 09:31:47ID: 9278794

Check out this webpage: http://www.hotscripts.com/PHP/Scripts_and_Programs/Image_Manipulation/index.html

There are many thumbnail solutions/tutorials here.

Good luck,
-Ron

 

by: lobangkinPosted on 2003-09-09 at 13:05:12ID: 9323715

<?php
header("Content-type: image/jpeg");
readfile(''/pics/sample.jpg");
?>


Yes it shows the image, and I took the header off, it shows the image also.

 

by: shmertPosted on 2003-09-09 at 13:27:41ID: 9323915

Does this show as garbage?
<?php
header("Content-type: text/plain");
readfile(''/pics/sample.jpg");
?>

 

by: lobangkinPosted on 2003-09-11 at 07:20:37ID: 9337199

>>>>>>>>>>.

Does this show as garbage?
<?php
header("Content-type: text/plain");
readfile(''/pics/sample.jpg");
?>
<<<<<<<<<<<<<<<<<<<<<


no it still show as a image..

nevermind.. I guess..

 

by: shmertPosted on 2003-09-11 at 07:36:14ID: 9337316

Sorry it isn't working, l.  Are you sure you're pointing your browser to the right php page?  If you set the header as text, then I don't think the image would display correctly.  Good luck with this!

-S

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...