php上传图片生成缩略图(GD库)

前端之家收集整理的这篇文章主要介绍了php上传图片生成缩略图(GD库)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先来一段简单的PHP上传图片生成缩略图的详细代码分享给大家供大家参考,具体内容如下

PHP;"> PHP function createThumbnail($imageDirectory,$imageName,$thumbDirectory,$thumbWidth,$quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type = preg_replace('@^.+(?<=/)(.+)$@','$1',$details['mime']); eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $thumbImg = imagecreatetruecolor($thumbWidth,$thumbHeight); imagecopyresampled($thumbImg,$srcImg,$thumbHeight,$details[0],$details[1]); eval('image'.$type.'($thumbImg,"$thumbDirectory/$imageName"'. (($type=='jpeg')?',$quality':'').');'); imagedestroy($srcImg); imagedestroy($thumbImg); } foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name,"data/$name"); createThumbnail("/location/of/main/image",$name,"/location/to/store/thumb",120,80); //120 = thumb width :: 80 = thumb quality (1-100) } } ?>

接下来再为大家推荐一个实例PHP使用GD库上传图片以及创建缩略图,直接看代码

GD库是

PHP进行图象操作一个很强大的库。

先在PHP.ini里增加一行引用:extension=PHP_gd2.dll

重启apache,做一个测试页var_dump(gd_info());输出数据表明GD库引用成功。

图片上传页面 upload.html

<a href="/tag/tupian/" target="_blank" class="keywords">图片</a><a href="/tag/shangchuan/" target="_blank" class="keywords">上传</a>

文件上传(只允许上传jpg类型图片)

PHP" method="post">

处理页面upload_img.PHP

PHP;"> PHP //上传图片保存地址 $uploadfile = "upfiles/".$_FILES['upfile']['name']; //缩略图保存地址 $smallfile = "upfiles/small_".$_FILES['upfile']['name'];

if($_FILES['upfile']['type'] != "image/jpeg")
{
echo '文件类型错误';
}
else
{
move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile); //上传文件

$dstW=200;//缩略图
$dstH=200;//缩略图

$src_image=ImageCreateFromJPEG($uploadfile);
$srcW=ImageSX($src_image); //获得图片
$srcH=ImageSY($src_image); //获得图片

$dst_image=ImageCreateTrueColor($dstW,$dstH);
ImageCopyResized($dst_image,$src_image,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($dst_image,$smallfile);

echo '文件上传成功
';
echo "";
}
?>

希望对大家学习PHP程序设计有所帮助,谢谢大家的支持

猜你在找的PHP相关文章