簡體   English   中英

PHP 圖像上傳到文件夾並制作縮略圖

[英]PHP Image upload to folder and making Thumbnail Images

我有這個代碼將文件上傳到一個文件夾(上傳)。 在文件 fileupload.php 中,我有一個類型為文件 multiple 的輸入。 我使用包含以下代碼的文件(upload.php)將文件(圖像)上傳到文件夾(上傳),這工作正常。 但我也想創建縮略圖以放入(上傳/拇指)。 而且我還需要設置這些縮略圖的大小。 謝謝你。

到目前為止我的代碼:

<?php
ready = "OK, Uppladdat";
uploaddir = "images/" . $imagepath;
uploaddir_th = "images/thumbs/" . $imagepath;
allowed = array('jpg','jpeg','gif','png');
max_size = 5048 * 1024;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['size'] <= $max_size) 
{
$file_ext  = pathinfo($_FILES[$key]['name'],PATHINFO_EXTENSION);
$file_name = basename($_FILES[$key]['name'],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed)) 
{
$name = $_FILES[$key]['name'];
$x = 1;
while (file_exists($uploaddir.'/'.$name)) 
{
   $name = $file_name.'['.$x.'].'.$file_ext;
   $x++;
}
if (move_uploaded_file($_FILES[$key]['tmp_name'],$uploaddir.'/'.$name))
{
   chmod($uploaddir.'/'.$name, 0644);
}
else
{
die(error_get_last());
}
}
else
{
   die("Invalid file type");
}
}
else
{
  die("File size too big");
}

//thumbnail image making part

list($width, $height) = getimagesize($uploaddir.'/'.$name);
$modwidth = 200;
$modheight = 140;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($uploaddir.'/'.$name);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight);
/* echo "Thumbnail: <img src='images/thumbs".$imagepath."'>"; */
imagejpeg($image, $uploaddir_th.'/'.$name, 100);
}
echo $ready; 
?>

您可以使用此 PHP 模塊並設置縮略圖的大小,並為您的圖像添加不同的效果http://php.net/manual/en/imagick.thumbnailimage.php

http://php.net/manual/en/book.imagick.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM