I am calling show_thumb to create thumbnails , but I am realizing that it is making the site load a little bit slower.
Below are two functions: show_thumb function that shows the thumbnail and make_thumb_gd that takes care of the thumbnail. Any suggestion on making it faster? The current approach makes the site slow, because those thumbnails are shown in the moving Jcarousel that contains the links returned by show_thumb.
Show thumb function
function show_thumb($file_org, $width, $height,$folder_version,$ratio_type = 'width')
{
$file_name = str_replace(SITE_WS_PATH."/", "", $file_org);
$file_name = str_replace("/", "^", $file_name);
$file_name = preg_replace("/[ ]+/", "_", $file_name);
$cache_file = $width."x".$height.'__'.$ratio_type .'__'.$file_name;
$folder_version = trim($folder_version);
if($folder_version =="english"){
$version ="";
}
else {
$version = $folder_version;
}
$file_fs_path = str_replace(SITE_WS_PATH, ROOT, $file_org);
if(!is_file(ROOT."/".$version."/".THUMB_CACHE_DIR."/".$cache_file)) {
$d = make_thumb_gd($file_fs_path, ROOT."/".$version."/".THUMB_CACHE_DIR."/".$cache_file, $width, $height, $ratio_type );
}else{
@unlink(ROOT."/".$version."/".THUMB_CACHE_DIR."/".$cache_file);
make_thumb_gd_1($file_fs_path, ROOT."/".$version."/".THUMB_CACHE_DIR."/".$cache_file, $width, $height, $ratio_type );
}
if($folder_version =="english"){
$link = SITE_WS_PATH."/".THUMB_CACHE_DIR."/".$cache_file;
}
else {
$link = SITE_WS_PATH."/".$folder_version."/".THUMB_CACHE_DIR."/".$cache_file;
}
return $link;
}
Make thumb function
function make_thumb_gd($imgPath, $destPath, $newWidth, $newHeight, $ratio_type = 'width', $quality = 60, $verbose = false)
{
// options for ratio type = width|height|distort|crop
// get image info (0 width and 1 height, 2 is (1 = GIF, 2 = JPG, 3 = PNG)
$size = @getimagesize($imgPath);
// break and return false if failed to read image infos
if (!$size) {
if ($verbose) {
echo "Unable to read image info.";
}
return false;
}
$curWidth = $size[0];
$curHeight = $size[1];
$fileType = $size[2];
// width/height ratio
$ratio = $curWidth/$curHeight;
$srcX = 0;
$srcY = 0;
$srcWidth = $curWidth;
$srcHeight = $curHeight;
if($ratio_type=='width') {
// If the dimensions for thumbnails are greater than original image do not enlarge
if($newWidth > $curWidth) {
$newWidth = $curWidth;
}
$newHeight = $newWidth / $ratio;
}else if($ratio_type=='crop') {
$thumbRatio = $newWidth / $newHeight;
if($ratio < $thumbRatio) {
$srcHeight = round($curHeight*$ratio/$thumbRatio);
$srcY = round(($curHeight-$srcHeight)/2);
} else {
$srcWidth = round($curWidth*$thumbRatio/$ratio);
$srcX = round(($curWidth-$srcWidth)/2);
}
/*echo "<br>curWidth: $curWidth";
echo "<br>curHeight: $curHeight";
echo "<br>newWidth: $newWidth";
echo "<br>newHeight: $newHeight";
echo "<br>ratio: $ratio";
echo "<br>thumbRatio: $thumbRatio";
echo "<br>srcWidth: $srcWidth";
echo "<br>srcX: $srcX";
echo "<br>srcHeight: $srcHeight";
echo "<br>srcY: $srcY";*/
} else if($ratio_type=='height') {
// If the dimensions for thumbnails are greater than original image do not enlarge
if($newHeight > $curHeight) {
$newHeight = $curHeight;
}
$newWidth = $newHeight * $ratio;
} else if($ratio_type=='distort') {
}
// create image
switch ($fileType) {
case 1:
if (function_exists("imagecreatefromgif")) {
$originalImage = imagecreatefromgif($imgPath);
} else {
if ($verbose) {
echo "GIF images are not support in this php installation.";
return false;
}
}
$fileExt = 'gif';
break;
case 2:
$originalImage = imagecreatefromjpeg($imgPath);
$fileExt = 'jpg';
break;
case 3:
$quality = 9;
$originalImage = imagecreatefrompng($imgPath);
$fileExt = 'png';
break;
default:
if ($verbose) {
echo "Not a valid image type.";
}
return false;
}
// create new image
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
//echo "$srcX, $srcY, $newWidth, $newHeight, $curWidth, $curHeight";
//echo "<br>$srcX, $srcY, $newWidth, $newHeight, $srcWidth, $srcHeight<br>";
imagecopyresampled($resizedImage, $originalImage, 0, 0, $srcX, $srcY, $newWidth, $newHeight, $srcWidth, $srcHeight);
switch ($fileExt) {
case 'gif':
imagegif($resizedImage, $destPath, $quality);
break;
case 'jpg':
imagejpeg($resizedImage, $destPath, $quality);
break;
case 'png':
$quality = 9;
imagepng($resizedImage, $destPath, $quality);
break;
}
// return true if successfull
return true;
}
@silencer will always slow your application down. I can also tell you, that if your using@you have made your code wrong, and need to back and fix it so that no@s are required. \$\endgroup\$make_thumb_gdwhen absolutely required. If the thumbnail has already been generated and cached then you don't need to regenerate it. No need tounlinkand recreate the work that has already been done, assuming the image hasn't changed since it's last thumbnail generation. \$\endgroup\$