shmert can you help? I can increase another 100 points.
Main Topics
Browse All Topics
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.
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.
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.
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.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
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.
No need to reinvent the wheel. Use this: http://mightystuff.net/thu
Clemens
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,
global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_COOKIE_VARS;
if (isset($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,$ne
}
/**
*
* private function - do not call
*
*/
function NewImgCreate($filename,$ne
$type = $this->GetImgType($filenam
if ($type=="png") {
$orig_img = imagecreatefrompng($filena
}
if ($type=="jpg") {
$orig_img = imagecreatefromjpeg($filen
}
$new_img =$this->NewImgResize($orig
if (!empty($fileout)) {
$this-> NewImgSave($new_img,$fileo
}
else {
$this->NewImgShow($new_img
}
ImageDestroy($new_img);
ImageDestroy($orig_img);
}
/**
*
* private function - do not call
*
*/
function NewImgResize($orig_img,$ne
$orig_size = getimagesize($filename);
if ($orig_size[0]<$orig_size[
$newxsize = $newysize * ($orig_size[0]/$orig_size[
}
else{
$newysize = $newxsize / ($orig_size[0]/$orig_size[
}
$im_out = ImageCreate($newxsize,$new
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,$fileo
if ($type=="png") {
if (substr($fileout,strlen($f
$fileout .= ".png";
return imagepng($new_img,$fileout
}
if ($type=="jpg") {
if (substr($fileout,strlen($f
$fileout .= ".jpg";
return imagejpeg($new_img,$fileou
}
}
/**
*
* 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/cla
// create new object
$neu = new Img2Thumb($filename,$newxs
?>
Then call the thumbnail like so.......
<img src="/images/full_size/sho
Works well for me
Using.........
GD Version 1.6.2 or higher
P.S. The script above originally came from http://www.phpclasses.org/
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.
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.
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);
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);
}
$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/bigI
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_
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);
?>
YEs I just want to display it to a browser but I got errors..
>>>>>>>>>>
Warning: imagecreatetruecolor(): Invalid image dimensions in /home/sample/public_html/s
<<<<<<<<<<<<<<
my $from = 'http://www.domain.com/pic
anything wrong?
<?php
#thumb.php
header("Content-type: image/jpg");
$from = ''http://www.domain.com/pi
$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);
?>
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);
?>
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.
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.j
?>
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.
Check out this webpage: http://www.hotscripts.com/
There are many thumbnail solutions/tutorials here.
Good luck,
-Ron
Business Accounts
Answer for Membership
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? :)