php gd搴撳嚱鏁扮敓鎴愰珮娓呯缉鐣ュ浘绋嬪簭_PHP鏁欑▼ | 甯涔嬪
include('resizeimage.php');
if(!empty($_post)){
echo($filename.".jpg?cache=".rand(0,999999));
}
?>
resizeimage.php 鏂囦欢
$filename="image.thumb";
// 鐢熸垚鍥剧墖鐨勫搴?
$resizewidth=400;
// 鐢熸垚鍥剧墖鐨勯珮搴?
$resizeheight=400;function resizeimage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$resizewidth=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$resizeheight=true;
}
if($resizewidth && $resizeheight){
if($widthratio $ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($resizewidth){
$ratio = $widthratio;
}elseif($resizeheight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
imagejpeg ($newim,$name . ".jpg");
imagedestroy ($newim);
}else{
imagejpeg ($im,$name . ".jpg");
}
}if($_files['image']['size']){
if($_files['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_files['image']['tmp_name']);
}
if($im){
if(file_exists("$filename.jpg")){
unlink("$filename.jpg");
}
resizeimage($im,$resizewidth,$resizeheight,$filename);
imagedestroy ($im);
}
}
?>
上一篇: hibernate一对一外键关联
推荐阅读
-
Ubuntu16.04搭建php5.6Web服务器环境
-
PHP版Mysql爆破小脚本
-
PHP示例演示发送邮件给某个邮箱
-
jquery+php后台实现省市区联动功能示例
-
PHP中number_format()函数的用法讲解
-
PHP中str_split()函数的用法讲解
-
PHP中quotemeta()函数的用法讲解
-
Laravel访问出错提示:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or di解决方法
-
CentOS下安装Memcached和PHP Memcached扩展
-
PHP创建word文档的方法(平台无关)