/    Sign up×
Community /Pin to ProfileBookmark

Automatic Thumbnailer

I am currently building an image upload section on my website. I was wondering if there is a free script, application, or tutorial of some sort that will automatically thumbnail an image when it is uploaded. Even if its not coded in php. Any Feedback is greatly appreciated thanks.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 02.2006 — Don't know if this is an option for you, but what I do is create thumbnails "on the fly" rather than saving them on the server. I find this helps to simplify site maintenance, since I don't have to worry about two image files for each image. The code I currently use:
[code=php]
<?php
class ScaleImage
{
// ATTRIBUTES
var $image;
var $imageData = array();
var $lastError;
var $debugLevel; // 0 = no user_error, 1 = warning, 2 = error

// METHODS

/*
void ScaleImage([int debug_level])
constructor
*/
function ScaleImage($image, $maxWidth, $maxHeight, $debugLevel = 0)
{
$this->debugLevel = $debugLevel;
if($this->setImageData($image))
{
if($this->calcRatio($maxWidth, $maxHeight))
{
$this->displayImage();
}
}
} // end constructor

/*
bool setImageData(str image)
*/
function setImageData($image)
{
$result = TRUE;
if(is_readable($image))
{
$imageData = @getimagesize($image);
if($imageData == FALSE)
{
$this->error("ScaleImage::setImageData(): not a valid image file");
$result = FALSE;
}
else
{
$this->image = $image;
$this->imageData = $imageData;
}
}
else
{
$this->error("ScaleImage::setImageData() - file not found");
$result = FALSE;
}
return($result);
} // end setImageData


/*
bool calcRatio(int pixel_width, int pixel_height)
*/
function calcRatio($maxWidth, $maxHeight)
{
$result = TRUE;
if(is_numeric($maxWidth) and is_numeric($maxHeight))
{
$maxWidth = (INT) round($maxWidth);
$maxHeight = (INT) round($maxHeight);
if($maxWidth < 1 or $maxHeight < 1)
{
$this->error("setMaxWidth(): max width or max height is < 1");
$result = FALSE;
}
else
{
if(!empty($this->imageData[0]) and !empty($this->imageData[1]))
{
$this->ratio = min($maxWidth / $this->imageData[0],
$maxHeight / $this->imageData[1],
1);
}
else
{
$this->error("ScaleImage::calcRatio() - invalid source image size");
$result = FALSE;
}
}
}
else
{
$this->error("setMaxWidth(): max width or height is not numeric");
$result = FALSE;
}
return($result);
} // end calcRatio()

/*
bool displayImage();
*/
function displayImage()
{
$result = TRUE;
$width = (INT) round($this->ratio * $this->imageData[0]);
$height = (INT) round($this->ratio * $this->imageData[1]);
if($width < 1 or $height < 1)
{
$this->error("ScaleImage::displayImage() - new width or height < 1");
$result = FALSE;
}
else
{
$newImage = imagecreatetruecolor($width, $height);
switch($this->imageData['mime'])
{
case "image/jpeg":
$oldImage = @imagecreatefromjpeg($this->image);
break;
case "image/png":
$oldImage = @imagecreatefrompng($this->image);
break;
case "image/gif":
$oldImage = @imagecreatefromgif($this->image);
break;
default:
$this->error("ScaleImage::displayImage() - invalid mime type");
$result = FALSE;
}
if($oldImage != FALSE)
{
if(imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $width, $height,
$this->imageData[0], $this->imageData[1]))
{
header("Content-type: " . $this->imageData['mime']);
switch($this->imageData['mime'])
{
case "image/jpeg":
imagejpeg($newImage, NULL, 100);
break;
case "image/png":
imagejpeg($newImage, NULL, 100);
break;
case "image/gif":
imagejpeg($newImage, NULL, 100);
break;
default:
$this->error("ScaleImage::displayImage() - invalid mime type");
$result = FALSE;
}
}
else
{
$this->error("ScaleImage::displayImage() - copy failed");
$result = FALSE;
}
}
else
{
$this->error("ScaleImage::displayImage() - create new image failed");
$result = FALSE;
}
}
} // end displayImage()

/*
void error(str error_message)
*/
function error($msg)
{
$this->lastError = $msg;
if($this->debugLevel)
{
$level = ($this->debugLevel == 2) ? E_USER_ERROR : E_USER_WARNING;
user_error($msg, $level);
}
} // end error()

} // end class ScaleImage

// do it:
if(!empty($_GET['image']) and !empty($_GET['width']) and !empty($_GET['height']))
{
$image = basename(htmlentities($_GET['image']));
$width = (INT) round($_GET['width']);
$height = (INT) round($_GET['height']);
$image = new ScaleImage($image, $width, $height, 1);
}
?>
[/code]

Usage:
[code=html]
<img src="/images/ScaleImage?image=photo_1.jpg&width=100&height=150 alt="">
[/code]

...where the width and height values are the maximum dimensions you want to allow. This line toward the end...
[code=php]$image = basename(htmlentities($_GET['image']));[/code]...results in the requirement that this script be in the same directory as the images. I did it that way to make the class only operable where I want it to be, but you could easily remove the basename part if you want it to be able to access any directories on your site.
Copy linkTweet thisAlerts:
@bokehAug 02.2006 — what I do is create thumbnails "on the fly" rather than saving them on the server.[/QUOTE]I've done it both ways. My opinion is this method is ok if [list]
  • [*]the image is unique and wont be used again

  • [*]it is only used for occasional images and only one or two per page

  • [*]the source images are already reasonably small

  • [/list]

    On the other hand if this were an image gallery displaying 100 thumbnails per page I guess thumbnailing on the fly would present an unacceptible load to the server.
    Copy linkTweet thisAlerts:
    @NogDogAug 02.2006 — I've done it both ways. My opinion is this method is ok if [list]
  • [*]the image is unique and wont be used again

  • [*]it is only used for occasional images and only one or two per page

  • [*]the source images are already reasonably small

  • [/list]

    On the other hand if this were an image gallery displaying 100 thumbnails per page I guess thumbnailing on the fly would present an unacceptible load to the server.[/QUOTE]

    Good point: I haven't had any reason to use it so far other than for a small number of images at a time.
    ×

    Success!

    Help @mateobus spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...