如何使用PHP批量上传时创建缩略图并将其保存到具有相同名称的子文件夹

时间:2019-02-09 22:44:38

标签: php image file upload thumbnails

我有一个php脚本,该脚本在目录中搜索以获取所有文件夹和图像,并获取其数据并将其保存到m mysql db。但是现在我想从抓取的图像创建php图像缩略图,这也为我找到了正确的解决方案,但是我不确定如何以正确的方式保护图像。在包含图像的主文件夹中的Thumbs文件夹中。这是我的代码有什么想法吗?

<?php


include_once("../assets/inc/colors.inc.php");

/* The image from which the palette will be generated */
$image_to_read = "img/footer.jpg";


/* Make sure that this number is a perfect square - 9,16,25,36 etc;
   this will enable you to create a square palette grid.

   The following for example will create a 5x5 grid.
 */

$colors_to_show = 6;

$pal = new GetMostCommonColors();
$pal->image = $image_to_read;
$colors = $pal->Get_Color();
$colors_key = array_keys($colors);


$inc = sqrt($colors_to_show);
 $imgcolors = "";
for ($i = 0; $i < $colors_to_show; $i += $inc) {


    for($j=0;$j<$inc;$j++) {
        $imgcolors .= "".$colors_key[$i + $j].",";
    }

}
$imgcolors .= "";
//echo $imgcolors;

/*------------------------------------------------------------------*/

$server = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'gbpics';

$connect = mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname,$connect);




$dirs = array_filter(glob('bilder/*'), 'is_dir');
foreach ($dirs as $dir) {
    $path  = $dir . "/";
    $files = array_map('mysql_real_escape_string', array_filter(glob("{$path}*.*"), 'is_file'));

    if (empty($files)) {
        echo "The data was inserted successfully.";
    } else {
        $insertValues = array();
        foreach ($files as $file) {

            $data           = getimagesize($file);
            $width          = $data[0];
            $height         = $data[1];
            $extension      = image_type_to_extension($data[2]);
            $imgsize        = filesize($file);


            if($ext == "gif"){ 
                $old_image = imagecreatefromgif($file);
            }else if($ext =="png"){ 
                $old_image = imagecreatefrompng($file);
            }else{ 
                $old_image = imagecreatefromjpeg($file);
            }


            $heightthumb = 380;
            $widththumb = 335;
            $thumb = imagecreatetruecolor($widththumb, $heightthumb);
            imagealphablending($thumb, false);
            imagesavealpha($thumb, true);
            imagecopyresampled($thumb, $old_image, 0, 0, 0, 0, $widththumb, $heightthumb, $data[0], $data[1]);







            $insertValues[] = "('Titel', '{$dir}', '{$file}', '$width', '$height', '$extension', '$imgsize', '$imgcolors', '$thumb')";
        }
        $query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`, `gbpicextension`, `gbpicsize`, `gbpiccolors`, `gbpicthumburl`) VALUES " . implode(', ', $insertValues);
        if (!mysql_query($query)) {
            echo "There was a problem inserting the data.";

            trigger_error("Query failed: $query<br />Error: " . mysql_error());
        } else {
            echo "The data was inserted successfully.";
        }
    }
}
?>

欢迎任何帮助。

0 个答案:

没有答案