High quality thumbnail generation function (multiple clipping modes, best scaling by height width, etc.)
Last Update:2017-02-28
Source: Internet
Author: User
function | thumbnail/**
* Scalable thumbnail generation function
* The latest version (registered user) can be obtained in the http://yodoo.com forum
* Demo effect also please login http://yodoo.com see, all of the site's thumbnails (jpg,png) are generated using this function
*
* Reprint Please keep the complete information
*
* @author Austin Chin <austinfay@hotmail.com> http://yodoo.com
* @version $Revision: 1.7 $
*
*
* Version
*
* + to indicate added functionality
*-to indicate discarded features
* C indicates a modified function
* E indicates extended functionality
*
* v1.5
* MAKETHUMB ($srcFile, $dstFile, $dstW, $dstH, $option =1)
*
* v1.6
* + Increase the cut mode
* + $option 8: Best Zoom width
* + $option 16: Height Best Zoom
* MAKETHUMB ($srcFile, $dstFile, $dstW, $dstH, $option =1, $cutmode =0, $startX = 0,
* $startY =0)
*
* v1.7
* E returns the value to an array, the first element is code 0 Normal, the other bit error code, and the second element is the error description.
* Error code:
*-1 source file does not exist.
*-2 Unsupported picture output function
*-3 Unsupported picture creation function
*-4 HTTP header information has been exported and the picture cannot be exported to the browser.
*-5 cannot detect the output of the picture type
* + Add function message2image can output string into picture format
*/
/**
* Scalable thumbnail generation function
*
* @param string $srcFile source file
* @param string $srcFile destination file
* @param int $dstW The width of the target picture (in pixels)
* @param int $dstH The height of the target picture (in pixels)
* @param int $option Additional parameters that can be added together, such as 1+2 (or 1|2), which represents the ability to perform both 1 and 2.
* 1: Default, output to the specified file 2: Picture content output to browser 4: Do not maintain picture proportions
* 8: Width Best Zoom 16: Height Best Zoom
* @param int $cutmode cut mode 0: Default mode, cut Mode 1: Left or Upper 2: Medium 3: Right or bottom
* @param the starting horizontal (pixel) of the int $startX shearing
* @param int $startY The start ordinate (pixel) of the cut
* @return Array return[0]=0: normal; RETURN[0] is error code RETURN[1] String: Description of Error
*/
Define (Op_to_file, 1); Output to the specified file
Define (Op_output, 2); Picture content output to browser
Define (Op_not_keep_scale, 4); Do not maintain picture proportions, even with stretching
Define (Op_best_resize_width, 8); Width Best Zoom
Define (Op_best_resize_height, 16); Height Best Zoom
Define (cm_default, 0); Default mode
Define (cm_left_or_top, 1); Left or up
Define (Cm_middle, 2); In
Define (Cm_right_or_bottom, 3); Right or bottom
function Makethumb ($srcFile, $dstFile, $dstW, $dstH, $option =op_to_file,
$cutmode =cm_default, $startX =0, $startY =0) {
$img _type = Array (1=> "gif", 2=> "JPEG", 3=> "PNG");
$type _idx = Array ("GIF" =>1, "jpg" =>2, "JPEG" =>2, "JPE" =>2, "PNG" =>3);
if (!file_exists ($srcFile)) {
Return Array ( -1, "Source File NOT exists: $srcFile.");
}
$path _parts = @pathinfo ($dstFile);
$ext = Strtolower ($path _parts["extension"]);
if ($ext = = "") {
Return Array ( -5, "Can" T detect output image ' s type. ");
}
$func _output = "image". $img _type[$type _idx[$ext]];
if (!function_exists ($func _output)) {
Return Array ( -2, "Function NOT exists for output: $func _output.");
}
$data = @GetImageSize ($srcFile);
$func _create = "Imagecreatefrom". $img _type[$data [2]];
if (!function_exists ($func _create)) {
Return Array ( -3, "Function NOT exists for create: $func _create.");
}
$im = @ $func _create ($srcFile);
$srcW = @ImageSX ($im);
$srcH = @ImageSY ($im);
$srcX = 0;
$srcY = 0;
$dstX = 0;
$dstY = 0;
if ($option & Op_best_resize_width) {
$dstH = Round ($dstW * $srcH/$srcW);
}
if ($option & Op_best_resize_height) {
$dstW = Round ($dstH * $srcW/$srcH);
}
$fdstW = $dstW;
$fdstH = $dstH;
if ($cutmode!= cm_default) {//Cut mode 1: Left or Upper 2:3: Right or bottom
$srcW-= $startX;
$srcH-= $startY;
if ($srcW * $dstH > $srcH * $dstW) {
$testW = Round ($dstW * $srcH/$dstH);
$testH = $srcH;
} else {
$testH = Round ($dstH * $srcW/$dstW);
$testW = $srcW;
}
Switch ($cutmode) {
Case Cm_left_or_top: $srcX = 0; $srcY = 0; Break
Case Cm_middle: $srcX = Round (($srcW-$testW)/2);
$srcY = Round (($srcH-$testH)/2); Break
Case Cm_right_or_bottom: $srcX = $srcW-$testW;
$srcY = $srcH-$testH;
}
$srcW = $testW;
$srcH = $testH;
$srcX + + $startX;
$srcY + + $startY;
else {//original zoom
if (!) ( $option & Op_not_keep_scale)) {
The following code calculates the new size and keeps the picture proportional
if ($srcW * $dstH > $srcH * $dstW) {
$fdstH =round ($srcH * $dstW/$srcW);
$dstY =floor (($dstH-$fdstH)/2);
$fdstW = $dstW;
} else {
$fdstW =round ($srcW * $dstH/$srcH);
$dstX =floor (($dstW-$fdstW)/2);
$fdstH = $dstH;
}
$dstX = ($dstX <0) 0: $dstX;
$dstY = ($dstX <0) 0: $dstY;
$dstX = ($dstX > ($dstW/2)) Floor ($dstW/2): $dstX;
$dstY = ($dstY > ($dstH/2)) floor ($dstH/s): $dstY;
}
}///End If ($cutmode!= cm_default) {//Cut mode
if (function_exists ("imagecopyresampled") and
Function_exists ("Imagecreatetruecolor")) {
$func _create = "Imagecreatetruecolor";
$func _resize = "imagecopyresampled";
} else {
$func _create = "Imagecreate";
$func _resize = "imagecopyresized";
}
$newim = @ $func _create ($dstW, $dstH);
$black = @ImageColorAllocate ($newim, 0,0,0);
$back = @imagecolortransparent ($newim, $black);
@imagefilledrectangle ($newim, 0,0, $dstW, $dstH, $black);
@ $func _resize ($newim, $im, $dstX, $dstY, $srcX, $srcY, $fdstW, $fdstH);
if ($option & Op_to_file) {
@ $func _output ($newim, $dstFile);
}
if ($option & Op_output) {
if (function_exists ("Headers_sent")) {
if (Headers_sent ()) {
Return Array ( -4, "HTTP already sent, can ' t output image to browser.");
}
}
Header ("content-type:image/". $img _type[$type _idx[$ext]));
@ $func _output ($newim);
}
@imagedestroy ($im);
@imagedestroy ($newim);
Return Array (0, "OK");
}