php实现从上传文件创建缩略图的方法
更新时间:2015年04月02日 11:29:44 作者:不吃皮蛋
这篇文章主要介绍了php实现从上传文件创建缩略图的方法,涉及php操作上传文件及图片操作的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php实现从上传文件创建缩略图的方法。分享给大家供大家参考。具体实现方法如下:
<?php if ($_REQUEST['action']=="add"){ $userfile = $HTTP_POST_FILES['photo']['tmp_name']; $userfile_name = $HTTP_POST_FILES['photo']['name']; $userfile_size = $HTTP_POST_FILES['photo']['size']; $userfile_type = $HTTP_POST_FILES['photo']['type']; ///////////////////////// //GET-DECLARE DIMENSIONS // $dimension = getimagesize($userfile); $large_width = $dimension[0]; // GET PHOTO WIDTH $large_height = $dimension[1]; //GET PHOTO HEIGHT $small_width = 120; // DECLARE THUMB WIDTH $small_height = 90; // DECLARE THUMB HEIGHT ///////////////////////// //CHECK SIZE // if ($userfile_size>102400){ $error=1; $msg = "The photo is over 100kb. Please try again."; } //////////////////////////////// // CHECK TYPE (IE AND OTHERS) // if ($userfile_type="image/pjpeg"){ if ($userfile_type!="image/jpeg"){ $error=1; $msg = "The photo must be JPG"; } } ////////////////////////////// //CHECK WIDTH/HEIGHT // if ($large_width!=600 or$large_height!=400){ $error=1; $msg = "The photo must be 600x400 pixels"; } /////////////////////////////////////////// //CREATE THUMB / UPLOAD THUMB AND PHOTO /// if ($error<>1){ $image = $userfile_name; //if you want to insert it to the database $pic = imagecreatefromjpeg($userfile); $small = imagecreatetruecolor($small_width,$small_height); imagecopyresampled($small,$pic,0,0,0,0, $small_width, $small_height, $large_width, $large_height); if (imagejpeg($small,"path/to/folder/to/upload/thumb".$userfile_name, 100)){ $large = imagecreatetruecolor($large_width,$large_height); imagecopyresampled($large,$pic,0,0,0,0, $large_width, $large_height, $large_width, $large_height); if (imagejpeg($large,"path/to/folder/to/upload/photo".$userfile_name, 100)) {} else {$msg="A problem has occured. Please try again."; $error=1;} } else { $msg="A problem has occured. Please try again."; $error=1; } } ////////////////////////////////////////////// /// If everything went right a photo (600x400) and /// a thumb(120x90) were uploaded to the given folders } ?> <html><head><title>create thumb</title></head> <body> <form name="form1" enctype="multipart/form-data" action="thisfile.php?action=add" method="post"> Select Photo: <input type="file" name="photo"> <input type="submit" name="submit" value="CREATE THUMB AND UPLOAD"> </form> </body </html>
希望本文所述对大家的php程序设计有所帮助。
相关文章
php使用curl_init()和curl_multi_init()多线程的速度比较详解
这篇文章主要介绍了php使用curl_init()和curl_multi_init()多线程的速度比较,结合实例形式详细分析了curl_init()和curl_multi_init()的具体使用方法及相关效率比较,需要的朋友可以参考下2018-08-08PHP中使用sleep造成mysql读取失败的案例和解决方法
这篇文章主要介绍了PHP中使用sleep造成mysql读取失败的案例和解决方法,如果遇到这个问题,可能会耗费你N久的时间,希望你能快速的搜索到这篇文章吧,需要的朋友可以参考下2014-08-08深入掌握include_once与require_once的区别
要深入掌握include(_once)与require(_once),需要掌握以下几点内容,不过有人建议不建议使用2013-06-06PHP中include/require/include_once/require_once使用心得
include() 、require()语句包含并运行指定文件。这两结构在包含文件上完全一样,唯一的区别是对于错误的处理。require()语句在遇到包含文件不存在,或是出错的时候,就停止即行,并报错。include()则继续即行。2016-08-08
最新评论