如何使用php调整图像文件的大小


How to resize an image file using php

我想做的是将每个图像的大小调整为300x300。我的问题是在我目前的代码一些图像文件是移动到上传文件夹是大。我想在上传文件夹中的所有图像文件的大小为300x300。

当前PHP代码:

<?php
include_once('../dbc/database.php');
$db = new Connection();
$db = $db->dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$emailCodeResult = isset($_POST['emailCodeResult']) ? $_POST['emailCodeResult'] : "";
$imageLink = isset($_POST['imageLink']) ? $_POST['imageLink'] : "";
const path = "Oppa/upload/";
$s= explode(path,$imageLink);
unlink("../upload/".$s[1]);
$email = isset($_POST['email']) ? $_POST['email'] : "";
$type = $_FILES["imageInput"]["type"];
$ext = end(explode('/', $type));
$filename = uniqid() . '_' .$emailCodeResult . '.' . $ext; 
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $filename);
$location = "Oppa/upload/" . $filename;
if(!empty($_POST['email'])) {
        $q = "UPDATE tbl_user SET user_image = '$location' WHERE user_email= :email ";
        $query = $db->prepare($q);
        $query->bindParam(':email', $email);
        $results = $query->execute();
        echo "1";
}
?>

下面是调整图像大小的代码。这对你有帮助吗?

             $path = getcwd();
             $oldpic = $path.'/images/test.jpg'; //your image path
             $array = explode("/",$oldpic);
             $count = count($array);
             $name = $array[$count-1];
             $src = $oldpic;
             $dest = $path."/images/thumbnail/".$name; // resized image
             //Genrating the image from there extension
             $size = getimagesize($src);
             switch($size["mime"]){
                        case "image/jpeg":
                          $source_image = imagecreatefromjpeg($src); //jpeg file
                        break;
                        case "image/gif":
                          $source_image = imagecreatefromgif($src); //gif file
                        break;
                        case "image/png":
                          $source_image = imagecreatefrompng($src); //png file
                        break;
                        default:
                          $source_image=false;
                        break;
             }
             $width = imagesx($source_image);
             $height = imagesy($source_image);
             $newwidth=300;
             $newheight= 300;
             $virtual_image = imagecreatetruecolor($newwidth, $newheight);
             imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
             imagejpeg($virtual_image,$dest,100);