【问题标题】:How to upload an image and create a thumbnail at the same time in PHP?如何在 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() 函数失败,因为它不能在路径中找到该特定文件名。我该如何解决?

【问题讨论】:

    标签: php thumbnails


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-31
      • 2011-10-30
      • 2016-09-14
      • 2013-04-08
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      相关资源
      最近更新 更多