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

PHP uploads images to generate thumbnails (GD library)

WBOY
Release: 2016-07-29 09:07:34
Original
782 people have browsed it

首先来一段简单的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)
}
}
?>
Copy after login

接下来再为大家推荐一个实例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' c />";
 }
?>
Copy after login

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

以上就介绍了php上传图片生成缩略图(GD库),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template