Home >Backend Development >PHP Tutorial >PHP uploads images to generate thumbnails (GD library), _PHP tutorial

PHP uploads images to generate thumbnails (GD library), _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:28991browse

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

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

<&#63;php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){
$details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.');
$type = preg_replace('@^.+(&#63;<=/)(.+)$@', '$1', $details['mime']);
eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,
$details[0], $details[1]);
eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.
(($type=='jpeg')&#63;', $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)
}
}
&#63;>

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

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

先在php.ini里增加一行引用:extension=php_gd2.dll

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

图片上传页面 upload.html

<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>图片上传</title>
</head>
<body>
<h1>文件上传(只允许上传jpg类型图片)</h1>
<form enctype="multipart/form-data" action="upload_img.php" method="post">
 <input name="upfile" type="file"><br><br>
 <input type="submit" value="提交">
</form>
</body>
</html>

处理页面upload_img.php

<&#63;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,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
  ImageJpeg($dst_image,$smallfile);

  echo '文件上传成功<br>';
  echo "<img src='$smallfile' />";
 }
&#63;>

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

您可能感兴趣的文章:

  • php ajax无刷新上传图片实例代码
  • 简单PHP上传图片、删除图片实现代码
  • php getimagesize 上传图片的长度和宽度检测代码
  • php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
  • 超级好用的一个php上传图片类(随机名,缩略图,加水印)
  • 基于php上传图片重命名的6种解决方法的详细介绍
  • php上传图片到指定位置路径保存到数据库的具体实现
  • php上传图片存入数据库示例分享
  • php实现上传图片生成缩略图示例
  • php实现按指定大小等比缩放生成上传图片缩略图的方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1089197.htmlTechArticlephp上传图片生成缩略图(GD库), 首先来一段简单的php上传图片生成缩略图的详细代码,分享给大家供大家参考,具体内容如下 phpfunction...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn