如何在 PHP 中同时上传图片和创建缩略图?

Posted

技术标签:

【中文标题】如何在 PHP 中同时上传图片和创建缩略图?【英文标题】:How to upload an image and create a thumbnail at the same time in PHP? 【发布时间】:2021-11-26 19:23:35 【问题描述】:

我要创建一个图片库网站。我需要调整和裁剪上传的图片,为每张图片创建一个缩略图。我使用了这些代码:

cropimage.php

<?php
class Crop

    public $img_name;
    public $tmp_img_name;
    public $folder;
    public $ext;
    public $new_name;


    function CropImage($file, $max_resolution)
    

        if (file_exists($file)) 
            $original_image = imagecreatefromjpeg($file);
            $original_width = imagesx($original_image);
            $original_height = imagesy($original_image);

            //Try max-width first
            if ($original_height > $original_width) 

                $ratio = $max_resolution / $original_width;
                $new_width = $max_resolution;
                $new_height = $original_height * $ratio;
             else 

                $ratio = $max_resolution / $original_height;
                $new_height = $max_resolution;
                $new_width = $original_width * $ratio;
            

            if ($original_image) 

                $new_image = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

                $new_crop_image = imagecreatetruecolor($max_resolution, $max_resolution);
                imagecopyresampled($new_crop_image, $new_image, 0, 0, 0, 0, $max_resolution, $max_resolution, $max_resolution, $max_resolution);

                imagejpeg($new_crop_image, $file, 90);
            
        
    


    public function RunCrop()
    
        $thumb_name_name = $this->img_name;
        $thumb_tmp_name = $this->tmp_img_name;
        $thumb_new_name = $this->new_name;
        $thumb_folder = "../public/assets/uploadThumb/";
        $file = $thumb_folder . $thumb_new_name;

        //Copy the original image to thumb folder
        copy($this->folder . $this->new_name, $file);

        //Resize file
        $this->CropImage($file, "300");
    

保存图片.php:

<?php

include "../app/core/config.php";
include "../app/core/cropImage.php";

class UploadImage

    private $ext = "";
    private $new_name = "";
    private $save_result = 0;

    public function UploadIMG()
    
        if (isset($_POST['img_submit'])) 
            try 

                //For uploading original image
                $crop = new Crop();

                $crop->img_name = $_FILES['image']['name'];
                $crop->tmp_img_name = $_FILES['image']['tmp_name'];
                $crop->folder = "../public/assets/img/";
                //For finding file extension
                $crop->ext = pathinfo($crop->img_name, PATHINFO_EXTENSION);
                //Renaming the uploaded file
                $crop->new_name = $this->RandomStringGenerator() . "." . $crop->ext;
                $this->new_name = $crop->new_name;
                //Moving to the desired path
                move_uploaded_file($crop->tmp_img_name, $crop->folder . $crop->new_name);
                $this->RegisterIntoDatabase();

                //For cropping image and creating thumbnail
                $crop->RunCrop();

                unset($_POST);
                $this->save_result = 1;
                return $this->save_result;
             catch (\Throwable $th) 

                $this->save_result = 2;
                return $this->save_result;
            
        
    
    public function RandomStringGenerator()
    
        $permitted_chars = "0123456789abcdefghijkl";
        $random_string = substr(str_shuffle($permitted_chars), 0, 10);
        return $random_string;
    
    public function RegisterIntoDatabase()
    
        require "../app/core/database.php";

        $bold_title = $_POST['boldtitle'];
        $title = $_POST['title'];
        $subtitle = $_POST['subtitle'];
        $image = $this->new_name;

        $sql = "INSERT INTO images (title_1, title_2, subtitle, image, thumb)
         VALUES ('$bold_title', '$title', '$subtitle', '$image', '$image')";
        $connection->query($sql);
    

问题是 RandomStringGenerator() 函数多次运行并生成两个不同的图像文件名并导致 RunCrop() 函数失败,因为它不能在路径中找到该特定文件名。我该如何解决?

【问题讨论】:

【参考方案1】:

在 XAMPP 设置中启用 php GD,解决了我的问题。

【讨论】:

以上是关于如何在 PHP 中同时上传图片和创建缩略图?的主要内容,如果未能解决你的问题,请参考以下文章

如何在视频上传时创建缩略图? [php/codeigniter]

图片上传有效,缩略图歪斜?

我在百度空间里上传照片时不能显示缩略图,而上传完以后不能显示文件名(我用的是IE7),请问如何解决?

生成缩略图的PHP上传图片类

php 图片操作类,支持生成缩略图,添加水印,上传缩略图

php视频上传和ffmpeg不会创建缩略图