當前位置:才華齋>計算機>php語言>

php上傳圖片生成縮圖

php語言 閱讀(1.46W)

如何用php上傳圖片生成縮圖,還闡述了利用GD庫上傳圖片以及建立縮圖,感興趣的小夥伴們可以參考一下。

php上傳圖片生成縮圖

首先來一段簡單的`php上傳圖片生成縮圖的詳細程式碼,分享給大家供大家參考,具體內容如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?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, 0, 0, 0, 0, $thumbWidth, $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)

}

}