php 上传图片并生成缩略图的解决办法
内容摘要
这篇文章主要为大家详细介绍了php 上传图片并生成缩略图的简单示例,具有一定的参考价值,可以用来参考一下。
php上传图片时生成缩略图,对此感兴趣的朋友,看看idc笔记做的技术笔
php上传图片时生成缩略图,对此感兴趣的朋友,看看idc笔记做的技术笔
文章正文
这篇文章主要为大家详细介绍了php 上传图片并生成缩略图的简单示例,具有一定的参考价值,可以用来参考一下。
php上传图片时生成缩略图,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下: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 | <code class = "php" > /** * 上传图片时生成缩略图 * * @param * @author php教程 www.idcnote.com **/ function createThumbnail( $imageDirectory , $imageName , $thumbDirectory , $thumbWidth , $quality ){ $details = getimagesize ( "$imageDirectory/$imageName" ) or die ( 'Please only upload images.' ); $type = preg_replace( '@^.+(?<=/)(.+)$@' , '$1' , $details [ 'mime' ]); eval ( '$srcImg = imagecreatefrom' . $type . '("$imageDirectory/$imageName");' ); $thumbHeight = $details [1] * ( $thumbWidth / $details [0]); $thumbImg = imagecreatetruecolor( $thumbWidth , $thumbHeight ); imagecopyresampled( $thumbImg , $srcImg , 0, 0, 0, 0, $thumbWidth , $thumbHeight , $details [0], $details [1]); eval ( 'image' . $type . '($thumbImg, "$thumbDirectory/$imageName"' . (( $type == 'jpeg' )? ', $quality' : '' ). ');' ); imagedestroy( $srcImg ); imagedestroy( $thumbImg ); } foreach ( $_FILES [ "pictures" ][ "error" ] as $key => $error ) { if ( $error == UPLOAD_ERR_OK) { $tmp_name = $_FILES [ "pictures" ][ "tmp_name" ][ $key ]; $name = $_FILES [ "pictures" ][ "name" ][ $key ]; move_uploaded_file( $tmp_name , "data/$name" ); createThumbnail( "/location/of/main/image" , $name , "/location/to/store/thumb" , 120, 80); //120 = thumb width :: 80 = thumb quality (1-100) } } /*** 代码来自php教程(www.idcnote.com) ***/ </code> |
注:关于php 上传图片并生成缩略图的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释