Hi there,
I would like to create thumbnails for all the images in a temp folder.
I wrote the following function based on the help provided from this forum.
From the main file called Image_Resize.php I call this resize function by
reading each and every file in the folder.
This works great under win 2k. But causes some problems in win NT
When the function is called from Image_Resize.php the error says that
"Notice: Undefined index: extension in E:\MySite\Include\CommonFunctions.php on line 328"
And I'm seeing all the binary funny characters of the image on the screen. Pls help me to solve this
Thanks
Ahmad
FileName: Functions.php
<?php
function imageCreateThumb($src,$dest,$maxWidth,$maxHeight,$quality=100) {
if (file_exists($src) && isset($dest)) {
// path info
$destInfo = pathInfo($dest);
$imageDetails = getimagesize($src);
$imageSizeX = $imageDetails[0];
$imageSizeY = $imageDetails[1];
$imageType = $imageDetails[2];
$thumbSizeX = $maxWidth;
$thumbSizeY = $maxHeight;
// The two lines beginning with (int) are the super important magic formula part.
(int)$thumbX = ($imageSizeX <= $imageSizeY) ? round(($imageSizeX * $thumbSizeY)/$imageSizeY) : $thumbSizeX;
(int)$thumbY = ($imageSizeX > $imageSizeY) ? round(($imageSizeY * $thumbSizeX)/$imageSizeX) : $thumbSizeY;
// path rectification
if ($destInfo['extension'] == "gif") {
$dest = substr_replace($dest, 'jpg', -3);
}
// true color image, with anti-aliasing
$destImage = imageCreateTrueColor($thumbX, $thumbY) or die ("Cannot Initialize new GD image stream");
imageAntiAlias($destImage,true);
// src image
switch ($imageType) {
case 1: //GIF
$srcImage = imageCreateFromGif($src);
break;
case 2: //JPEG
$srcImage = imageCreateFromJpeg($src);
break;
case 3: //PNG
$srcImage = imageCreateFromPng($src);
break;
default:
return false;
break;
}
// resampling
imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $thumbX, $thumbY, $imageSizeX, $imageSizeY);
// generating image
switch ($imageType) {
case 1:
case 2:
imageJpeg($destImage, $dest, $quality);
break;
case 3:
imagePng($destImage, $dest);
break;
}
return true;
}
else {
return false;
}
}
=============================================================================
FileName: ImageResize.php
<?
$TempFolder = realpath("ProductImages/Temp/");
$FileNamesArray = WriteFolderFilesToArray($TempFolder);
if ($FileNamesArray != "" ){
foreach ($FileNamesArray as $key => $FileName) {
//************** THUMB IMAGE CREATION *********************
$DestFolder = "ProductImages/Thumb";
$SourceFile = realpath($TempFolder . "/" . $FileName);
$DestFile = realpath($DestFolder . "/" . $FileName);
$maxWidth = 100;
$maxHeight = 80;
imageCreateThumb($SourceFile, $DestFile, $maxWidth, $maxHeight);
unlink($TempFolder . "/" . $FileName);
}
}
?>