php實(shí)現(xiàn)從上傳文件創(chuàng)建縮略圖的方法
更新時間:2015年04月02日 11:29:44 作者:不吃皮蛋
這篇文章主要介紹了php實(shí)現(xiàn)從上傳文件創(chuàng)建縮略圖的方法,涉及php操作上傳文件及圖片操作的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實(shí)例講述了php實(shí)現(xiàn)從上傳文件創(chuàng)建縮略圖的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<?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程序設(shè)計(jì)有所幫助。
相關(guān)文章
Look And Say 序列php實(shí)現(xiàn)代碼
Look And Say序列,簡單的說就是根據(jù)你看到的數(shù)字,寫出下一個數(shù)字2011-05-05
利用PHP如何統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于如何利用PHP統(tǒng)計(jì)Nginx日志的User Agent數(shù)據(jù)的相關(guān)資料,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
如何用PHP實(shí)現(xiàn)分布算法之一致性哈希算法
進(jìn)行大型網(wǎng)站的web開發(fā)時,分布式這個詞經(jīng)常出現(xiàn)在我們面前。如: memcache、redis服務(wù)器等緩存服務(wù)器的負(fù)載均衡(分布式cache)、 MySQL的分布式集群,這些都會用到分布式的思想,都要理解分布式算法。接下來以緩存服務(wù)器的負(fù)載均衡來談一下一致性哈希算法。2021-05-05

