Heim >php教程 >PHP源码 >php简单实用生成缩略图代码

php简单实用生成缩略图代码

WBOY
WBOYOriginal
2016-06-08 17:26:381095Durchsuche

本程序是根据用户上传图片后再把上传的图片按比例生成缩略图

<script>ec(2);</script>
 代码如下 复制代码


$w?$resizewidth=$w:$resizewidth=400;// 生成图片的宽度
$h?$resizeheight=$h:$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);
        imagedestroy ($newim);
    }else{
        imagejpeg ($im,$name);
    }
}

if($_files['uploadfile']['size']){
    if($_files['uploadfile']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($_files['uploadfile']['tmp_name']);
    }elseif($_files['uploadfile']['type'] == "image/x-png"){
        $im = imagecreatefrompng($_files['uploadfile']['tmp_name']);
    }elseif($_files['uploadfile']['type'] == "image/gif"){
        $im = imagecreatefromgif($_files['uploadfile']['tmp_name']);
    }
    if($im){
        if(file_exists('bbs.jpg')){
            unlink('bbs.jpg');
        }
        resizeimage($im,$resizewidth,$resizeheight,'bbs.jpg');
        imagedestroy ($im);
  
    }
}
//$uploadfile="www.111cn.net.jpg";
?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:经典php分页代码与分页原理(1/3)Nächster Artikel:php 模版设计模