The usage is as follows:
Export aw.thumbimage('1.jpg ', '2.jpg'); // save 1.jpg to, and save it as 2.jpg print_r ($ );
Print result:
Array (
[Status] => 0
[File] => 1.jpg
[Width] = & gt; 200
[Height] = & gt; 200
[SavePath] => 2.jpg
[Info] => the thumbnail is successfully generated.
)
Export aw.thumbimage('1.jpg ', 200,100); // set 1.jpg to in width and in height, and directly overwrite 1.jpg without generating new images.
The function code is as follows:
<? Php/** php function for generating image thumbnails * www.phpernote.com */function thumbImage ($ file, $ thumbWidth, $ thumbHeight, $ savePath = '', $ isCut = true, $ quality = 100) {$ result = array ('status' => 0, 'file' => '', 'width' => 0, 'height' => 0, 'savepath' => '', 'info' =>''); if (! File_exists ($ file) {return array ('status' => 1, 'file' => '', 'width' => 0, 'height' => 0, 'savepath' => '', 'info' => 'image file does not exist');} // check the file type $ fp = fopen ($ file, 'rb '); $ str = fread ($ fp, 2); // read-only 2 bytes fclose ($ fp); $ str = @ unpack ("c2chars", $ str ); $ typeCode = intval ($ str ['chars1']. $ str ['chars2']); $ fileType = ''; switch ($ typeCode) {case 255216: $ fileType = 'jpg '; break; case 7173: $ fileType = 'gif'; break; case 6677: $ fileType = 'bmp '; break; case 13780: $ f IleType = 'png '; break; default: $ fileType = $ typeCode; break ;} if ($ str ['chars1'] = '-1' & $ str ['chars2'] ='-40') {$ fileType = 'jpg ';} if ($ str ['chars1'] = '-119' & $ str ['chars2'] = '80') {$ fileType = 'png ';} if (! In_array ($ fileType, array ('jpg ', 'gif', 'bmp', 'png ') {return array ('status' => 2, 'file' => '', 'width' => 0, 'height' => 0, 'savepath' => '', 'info' => 'incorrect image file type :'. $ fileType);} // image scaling if ($ fileType = 'jpg ') {$ im = imagecreatefromjpeg ($ file );} if ($ fileType = 'gif') {$ im = imagecreatefromgif ($ file);} if ($ fileType = 'png ') {$ im = imagecreatefrompng ($ file);} if (empty ($ savePath) $ savePath = $ file; $ width = imagesx ($ im ); $ height = imagesy ($ im); if ($ width <$ thumbWidth & $ height <$ thumbHeight) {return array ('status' => 3, 'file' => '', 'width' => 0, 'height' => 0, 'savepath' => '', 'info' => 'image size smaller than the size of the generated thumbnail ');} $ ratio = $ width/$ height; // ratio of the actual image $ thumbRatio = $ thumbWidth/$ thumbHeight; // ratio of the changed image if ($ isCut) {// cropping if (function_exists ('imagepng ') & (str_replace ('. ', '', PHP_VERSION)> = 512) {// processing status after the php version is greater than 5.12 parameter change $ quality = 9 ;} if ($ ratio >=$ thumbRatio) {// high priority $ newimg = imagecreatetruecolor ($ thumbWidth, $ thumbHeight); imagecopyresampled ($ newimg, $ im, $ thumbWidth, $ thumbHeight, ($ height) * $ thumbRatio), $ height); imagejpeg ($ newimg, $ savePath, $ quality );} if ($ ratio <$ thumbRatio) {// width precedence $ newimg = trim ($ thumbWidth, $ thumbHeight); imagecopyresampled ($ newimg, $ im, 0, 0, 0, $ thumbWidth, $ thumbHeight, $ width, ($ width)/$ thumbRatio); imagejpeg ($ newimg, $ savePath, $ quality );}} else {// if ($ ratio >=$ thumbRatio) {$ newimg = imagecreatetruecolor ($ thumbWidth, ($ thumbWidth)/$ ratio); imagecopyresampled ($ newimg, $ im, 0, 0, 0, $ thumbWidth, ($ thumbWidth)/$ ratio, $ width, $ height); imagejpeg ($ newimg, $ savePath, $ quality );} if ($ ratio <$ thumbRatio) {$ newimg = imagecreatetruecolor ($ thumbHeight) * $ ratio, $ thumbHeight); imagecopyresampled ($ newimg, $ im, 0, 0, 0, ($ thumbHeight) * $ ratio, $ thumbHeight, $ width, $ height); imagejpeg ($ newimg, $ savePath, $ quality) ;}} ImageDestroy ($ im ); // www.phpernote.com/php-function/1007.htmlreturn array ('status' => 0, 'file' => $ file, 'width' => $ thumbWidth, 'height' => $ thumbHeight, 'savepath' => $ savePath, 'info' => 'thumbnail generated successfully ');}
Articles you may be interested in
- How php generates image thumbnails
- PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
- PHP random password generation function
- Php retrieves and outputs a list Of all images in the content
- Php extracts the birthday date from the ID card number and verifies whether it is a minor.
- Php generates a bar chart (Statistical Chart) Program
- Php verification code generation function
- Php idea and implementation method for generating short URLs