Hi Maarten,
Thanks for the reply.. But I don’t want to have a fixed size for all the images.
If I set a size say (w=500 h=450) what will happen to an image which is just 300px in width and 200px in height?? Won’t it get stretched?? Or if try to compress a big image I think it would be the same problem.
Below here I attached the PHP code which I use for resizng an image based on the calculation. The same thing I want’t to achieve in Servoy too.
What I do here I will define the max ht and width. After getting size (w,h) of the image the function will check
-
whether the ht is > width and also exceeds the max ht. If so it will resize the image with the max ht specified and change the width proportionally.
-
similarly if the width is > ht and also exceeds the max width then it will resize the image with the max width specified and change the height proportionally.
I think I’m explaining properly. If not pls let me know
Thanks
Ahmad
Hong Kong
<?
// *******************************************************
//---------------------------- Reszing Function --------------------
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;
}
}
// **********************************************************
//---------------------------- Reszing Images Starts Here --------------------
$TempFolder = realpath("Data/ProductImages/Temp/");
$FileNamesArray = WriteFolderFilesToArray($TempFolder);
if ($FileNamesArray != "" ){
foreach ($FileNamesArray as $key => $FileName) {
//************** THUMB IMAGE CREATION *********************
$DestFolder = "Data/ProductImages/Thumb";
$SourceFile = realpath($TempFolder . "/" . $FileName);
$DestFile = realpath($DestFolder . "/" . $FileName);
$maxWidth = 100;
$maxHeight = 80;
imageCreateThumb($SourceFile, $DestFile, $maxWidth, $maxHeight);
//************** NORMAL IMAGE CREATION *********************
$DestFolder = "Data/ProductImages/Normal";
$SourceFile = realpath($TempFolder . "/" . $FileName);
$DestFile = realpath($DestFolder . "/" . $FileName);
$maxWidth = 200;
$maxHeight = 200;
imageCreateThumb($SourceFile, $DestFile, $maxWidth, $maxHeight);
//************** BIG IMAGE CREATION *********************
$DestFolder = "Data/ProductImages/Big";
$SourceFile = realpath($TempFolder . "/" . $FileName);
$DestFile = realpath($DestFolder . "/" . $FileName);
$maxWidth = 500;
$maxHeight = 500;
imageCreateThumb($SourceFile, $DestFile, $maxWidth, $maxHeight);
}
}
?>