如何在文件上载期间移动已调整大小的图像

时间:2016-09-15 07:08:53

标签: php

我希望将上传的图片调整为宽度:750和高度:930像素。 这是php:

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["cert"]["name"]);
if (strtolower($imageFileType) == 'jpg' ){
    $check = getimagesize($_FILES["cert"]["tmp_name"]);
    $wd = $check[0];
    $ht = $check[1];
    $wd_n = 750;
    $ht_n = 930;
    $src = imagecreatefromjpeg($_FILES["cert"]["tmp_name"]);
    $dst = imagecreatetruecolor($wd_n, $ht_n);
    imagesavealpha($src, true);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $wd_n, $ht_n, $wd, $ht);
}

现在,我希望将此文件移至上传文件夹。如果没有调整大小,我可以这样做:

move_uploaded_file($_FILES["cert"]["tmp_name"], $target_file);

如何将新调整大小的文件移动到上传文件夹?

3 个答案:

答案 0 :(得分:0)

您可以使用 imagejpeg 功能

$path = <Path to directory + filename>
imagejpeg($dst, $path);
  

$ dst应该是一个图像资源,在你的情况下。

答案 1 :(得分:0)

您是否愿意尝试这样:

<?php
    $target_dir     = "upload/";
    $target_file    = $target_dir . basename($_FILES["cert"]["name"]);

    if (strtolower($imageFileType) == 'jpg' ){
        $check  = getimagesize($_FILES["cert"]["tmp_name"]);

        // SAVE THE ORIGINAL, UPLOADED IMAGE TO DISK
        move_uploaded_file($_FILES["cert"]["tmp_name"], $target_file);

        $wd     = $check[0];
        $ht     = $check[1];
        $wd_n   = 750;
        $ht_n   = 930;

        // GET THAT ORIGINAL IMAGE & PROCESS IT
        $src    = imagecreatefromjpeg($target_file);
        $dst    = imagecreatetruecolor($wd_n, $ht_n);

        imagesavealpha($src, true);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $wd_n, $ht_n, $wd, $ht);

        // NOW; OVERRIDE THE ORIGINAL AND SAVE THE RESIZED VERSION...
        imagejpeg($dst, $target_file, 100);

        // OR SAVE IT TO A NEW LOCATION...
        $newLocation = str_replace("upload/", "upload/resized/", $target_file);
        imagejpeg($dst, $newLocation, 100);
    }

答案 2 :(得分:0)

$dst之后,您可以使用imagejpeg。

imagejpeg($dst, "newPath/image.png");

干杯。