php 生成缩略图的示例
内容摘要
这篇文章主要为大家详细介绍了php 生成缩略图的示例,具有一定的参考价值,可以用来参考一下。
源文件,目标文件,目标宽,目标高,是否允许剪裁。如果目标写入 null 直接二进制输出。
源文件,目标文件,目标宽,目标高,是否允许剪裁。如果目标写入 null 直接二进制输出。
文章正文
这篇文章主要为大家详细介绍了php 生成缩略图的示例,具有一定的参考价值,可以用来参考一下。
源文件,目标文件,目标宽,目标高,是否允许剪裁。如果目标写入 null 直接二进制输出。不生成文件1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <code class = "php" > function img2thumb( $src_img , $dst_img , $width = 64, $height = 64, $nocut = 0) { if (! is_file ( $src_img )) return false; if (!( $width * $height )) return false; $ext = strtolower ( substr ( strrchr ( $src_img , '.' ),1)); if (! $ext ) return false; $otfunc = 'image' . ( $ext == 'jpg' ? 'jpeg' : $ext ); if (!function_exists( $otfunc )) return false; $srcinfo = getimagesize ( $src_img ); if (! $srcinfo ) return false; $src_w = $srcinfo [0]; $src_h = $srcinfo [1]; $type = strtolower ( substr (image_type_to_extension( $srcinfo [2]), 1)); $openfunc = 'imagecreatefrom' . ( $type == 'jpg' ? 'jpeg' : $type ); if (!function_exists( $openfunc )) return false; $createfunc = 'imagecreate' ; $buildfunc = 'imagecopyresized' ; $x = $y =0; $src_t = $src_w / $src_h ; if ( $src_t > $width / $height ) { $dst_w = $width ; $dst_h = $width / $src_t ; $nocut && $y = ( $height - $dst_h )/2; } else { $dst_w = $src_t * $height ; $dst_h = $height ; $nocut && $x = ( $width - $dst_w )/2; } $src = $openfunc ( $src_img ); $dst = $createfunc ( $nocut ? $width : $dst_w , $nocut ? $height : $dst_h ); $white = imagecolorallocate( $dst , 255, 255, 255); $buildfunc ( $dst , $src , $x , $y , 0, 0, $dst_w , $dst_h , $src_w , $src_h ); $otfunc ( $dst , $dst_img ); imagedestroy( $dst ); imagedestroy( $src ); return true; } /*** 来自php教程(www.idcnote.com) ***/ </code> |
注:关于php 生成缩略图的示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释