PHP编程:php实现从上传文件创建缩略图的方法
发布时间:2020-12-13 02:32:42 所属栏目:PHP教程 来源:网络整理
导读:《php实现从上传文件创建缩略图的方法》要点: 本文介绍了php实现从上传文件创建缩略图的方法,希望对您有用。如果有疑问,可以联系我们。 PHP实战 本篇章节讲解php实现从上传文件创建缩略图的办法.供大家参考研究.具体实现办法如下: PHP实战 ?phpi
《php实现从上传文件创建缩略图的方法》要点: 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,$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,$large_height,$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程序设计有所赞助. 《php实现从上传文件创建缩略图的方法》是否对您有启发,欢迎查看更多与《php实现从上传文件创建缩略图的方法》相关教程,学精学透。编程之家 52php.cn为您提供精彩教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |