PHP根据图片制作缩略图

2020-12-13 05:23

阅读:419

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:des   style   c   class   blog   code   

php中制作缩略图的方法也很简单,是用imagecopyresampled方法根据源图制作一个小一点的图片,来看代码check_image_addthumbs.php

soscw.com,搜素材
php 
//修改图片效果
$db = mysql_connect(‘localhost‘,‘root‘,‘Ctrip07185419‘) or die(‘can not connect to database‘);
mysql_select_db(‘moviesite‘,$db) or die(mysql_error($db));
//上传文件的路径
$dir = ‘D:\Serious\phpdev\test\images‘;
//缩略图的路径
$thumbdir = ‘D:\Serious\phpdev\test\images\thumbs‘;
//设置环境变量
putenv(‘GDFONTPATH=‘.‘C:\Windows\Fonts‘);
$font = "arial";

//upload_image.php页面传递过来的参数,如果是上传图片
if($_POST[‘submit‘] == ‘Upload‘)
{
    if($_FILES[‘uploadfile‘][‘error‘] != UPLOAD_ERR_OK)
    {
        switch($_FILES[‘uploadfile‘][‘error‘])
        {
            case UPLOAD_ERR_INI_SIZE:
                die(‘The uploaded file exceeds the upload_max_filesize directive‘);
            break;
            case UPLOAD_ERR_FORM_SIZE:
                die(‘The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form‘);
            break;
            case UPLOAD_ERR_PARTIAL:
                die(‘The uploaded file was only partially uploaded‘);
            break;
            case UPLOAD_ERR_NO_FILE:
                die(‘No file was uploaded‘);
            break;
            case UPLOAD_ERR_NO_TMP_DIR:
                die(‘The server is missing a temporary folder‘);
            break;    
            case UPLOAD_ERR_CANT_WRITE:
                die(‘The server fail to write the uploaded file to the disk‘);
            break;        
            case UPLOAD_ERR_EXTENSION:
                die(‘The upload stopped by extension‘);
            break;                
        }
    }
    $image_caption = $_POST[‘caption‘];
    $image_username = $_POST[‘username‘];
    $image_date = date(‘Y-m-d‘);
    list($width,$height,$type,$attr) = getimagesize($_FILES[‘uploadfile‘][‘tmp_name‘]);
    $error = ‘The file you upload is not a supported filetype‘;
    switch($type)
    {
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($_FILES[‘uploadfile‘][‘tmp_name‘]) or die($error);
        break;
        case IMAGETYPE_JPEG:
            $image = imagecreatefromjpeg($_FILES[‘uploadfile‘][‘tmp_name‘]) or die($error);
        break;
        case IMAGETYPE_PNG:
            $image = imagecreatefrompng($_FILES[‘uploadfile‘][‘tmp_name‘]) or die($error);
        break;
        default:
        break;
    }
    $query = ‘insert into images(image_caption,image_username,image_date) values("‘.$image_caption.‘" , "‘.$image_username.‘","‘.$image_date.‘")‘;
    $result = mysql_query($query,$db) or die(mysql_error($db));
    $last_id = mysql_insert_id();
    
    // $imagename = $last_id.‘.jpg‘;
    // imagejpeg($image,$dir.‘/‘.$imagename);
    // imagedestroy($image);
    
    $image_id = $last_id;
    imagejpeg($image , $dir.‘/‘.$image_id.‘.jpg‘);
    imagedestroy($image);
}
else  //如果图片已经上传,则从数据库中取图片名字
{
    $query = ‘select image_id,image_caption,image_username,image_date from images where image_id=‘.$_POST[‘id‘];
    $result = mysql_query($query,$db) or die(mysql_error($db));
    extract(mysql_fetch_assoc($result));
    list($width,$height,$type,$attr) = getimagesize($dir.‘/‘.$image_id.‘.jpg‘);
}

//如果是保存图片
if($_POST[‘submit‘] == ‘Save‘)
{
    if(isset($_POST[‘id‘]) && ctype_digit($_POST[‘id‘]) && file_exists($dir.‘/‘.$_POST[‘id‘].‘.jpg‘))
    {
        $image = imagecreatefromjpeg($dir.‘/‘.$_POST[‘id‘].‘.jpg‘);
    }
    else
    {
        die(‘invalid image specified‘);
    }
    $effect = (isset($_POST[‘effect‘])) ? $_POST[‘effect‘] : -1;
    switch($effect)
    {
        case IMG_FILTER_NEGATE:
            imagefilter($image , IMG_FILTER_NEGATE);     //将图像中所有颜色反转
        break;
        case IMG_FILTER_GRAYSCALE:
            imagefilter($image , IMG_FILTER_GRAYSCALE);  //将图像转换为灰度的
        break;
        case IMG_FILTER_EMBOSS:
            imagefilter($image , IMG_FILTER_EMBOSS);     //使图像浮雕化
        break;
        case IMG_FILTER_GAUSSIAN_BLUR:
            imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像
        break;    
    }
    
    if(isset($_POST[‘emb_caption‘]))
    {
        imagettftext($image , 12 , 0 , 20 , 20 , 0 , $font , $image_caption);
    }
    
    $thumb_width = $width * 0.10;
    $thumb_height = $height * 0.10;
                   
    //创建一个缩略图
    $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
    imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height,    $width, $height);
    //保存原图
    imagejpeg($image, $dir . ‘/‘ . $_POST[‘id‘] . ‘.jpg‘, 100);
    //保存缩略图
    imagejpeg($thumb, $thumbdir . ‘/‘ . $_POST[‘id‘] . ‘.jpg‘, 100);
    imagedestroy($thumb);
    ?>Here is your pic!

Your image has been saved!

php } else { ?> Here is your pic!

So how does it feel to be famous?

Here is the picture you just uploaded to your servers:

php if($_POST[‘submit‘] == ‘Upload‘) { $imagename = ‘images/‘.$image_id.‘.jpg‘; } else { $imagename = ‘image_effect.php?id=‘.$image_id.‘&e=‘.$_POST[‘effect‘]; if(isset($_POST[‘emb_caption‘])) { $imagename .= ‘&capt=‘.urlencode($image_caption); } } ?>
Image save as: $image_id?>
Height: echo $height;?>
Widht: echo $width;?>
Upload date: echo $image_date;?>

You may apply a special effect to your image from the list of option below. Note:saving an image with any of the filters applied can be undone

Filter:
php echo; if(isset($_POST[‘emb_caption‘])) { echo ‘ checked="checked"‘; } echo ‘ />Embed caption in image?‘; ?>

php } ?>
soscw.com,搜素材

缩略图放在D:\Serious\phpdev\test\images\thumbs路径下面,这里我们添加了一个gallery.php文件来陈列所有的缩略图,代码如下:

soscw.com,搜素材
php 
$db = mysql_connect(‘localhost‘,‘root‘,‘Ctrip07185419‘) or die(‘can not connect to database‘);
mysql_select_db(‘moviesite‘,$db) or die(mysql_error($db));

$dir = ‘images‘;
$thumbdir = $dir.‘/thumbs‘;
?>

    Welcome to our Photo Gallery

Click on any image to see it full sized.

php $result = mysql_query(‘select * from images‘) or die(mysql_error($db)); $odd = true; while($row = mysql_fetch_assoc($result)) { echo ($odd == true) ? ‘‘ : ‘‘; $odd = !$odd; extract($row); echo ‘‘; echo ‘‘; echo ‘‘; echo ‘‘; echo ‘‘; } ?>
Image Caption Uploaded By Date uploaded
; echo; echo‘.$image_caption.‘‘.$image_username.‘‘.$image_date.‘
soscw.com,搜素材

来看看最后显示缩略图的效果:

soscw.com,搜素材

注意下面的代码:

soscw.com,搜素材
    $thumb_width = $width * 0.10;
    $thumb_height = $height * 0.10;
                   
    //创建一个缩略图
    $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
    imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height,    $width, $height);
    //保存原图
    imagejpeg($image, $dir . ‘/‘ . $_POST[‘id‘] . ‘.jpg‘, 100);
    //保存缩略图
    imagejpeg($thumb, $thumbdir . ‘/‘ . $_POST[‘id‘] . ‘.jpg‘, 100);
    imagedestroy($thumb);
soscw.com,搜素材

我们设置缩略图的长度是原来长度的10%,宽度也是原来宽度的10%,注意保持一致,否则图片会出现失真,当然这个参数如果设置成大于1的整数,图片就会放大。

 

PHP根据图片制作缩略图,搜素材,soscw.com

PHP根据图片制作缩略图

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/tylerdonet/p/3736281.html


评论


亲,登录后才可以留言!