简体   繁体   English

如何轻松调整大小和重命名上载的图像? (php)

[英]How do I easily resize and rename images on upload? (php)

What code would I use to: 我将使用什么代码:

  1. Resize all images being uploaded to 600px width while maintaining aspect ratio for height 将所有上传的图片调整为600px的宽度,同时保持高宽比
  2. Rename the file being uploaded to the current time-stamp 重命名要上传到当前时间戳的文件

and how exactly would I add that to or edit my existing code (preferably without using classes): 以及如何精确地将其添加到或编辑现有代码(最好不使用类):

$target_dir2 = "creature_pics/";
$target_file2 = $target_dir2 . basename($_FILES["u_c2pic"]["name"]);
$uploadOk2 = 1;
$imageFileType2 = pathinfo($target_file2,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {
    $check2 = getimagesize($_FILES["u_c2pic"]["tmp_name"]);
    if($check2 !== false) {
        $uploadOk2 = 1;
    } else {
        $uploadOk2 = 0;
    }
}

if (file_exists($target_file2)) {
    $uploadOk2 = 0;
}

if ($_FILES["u_c2pic"]["size"] > 5000000) {
    $uploadOk2 = 0;
}

if($imageFileType2 != "jpg" && $imageFileType2 != "png" && $imageFileType2 != "jpeg"
&& $imageFileType2 != "gif" ) {
    $uploadOk2 = 0;
}

if ($uploadOk2 == 0) {
    $ercode2 = "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["u_c2pic"]["tmp_name"], $target_file2)) {
    } else {
        $ercode2 = "Sorry, there was an error uploading your file.";
    }
}

I'm new to php coding and would like the simplest solution possible. 我是php编码的新手,并且希望有最简单的解决方案。

If you can use Imagick on your server then the following code should help you out. 如果可以在服务器上使用Imagick,则以下代码应为您提供帮助。

First the function to do the resizing. 首先该功能进行大小调整。

if(!defined('APPLICATION_PATH')) define('APPLICATION_PATH',  dirname(__FILE__));

function popupImage($width, $height, $code, $name) {
    $file = APPLICATION_PATH.'/creature_pics/'.$name.'.jpg';

    $im = new Imagick($file);

    $imageprops = $im->getImageGeometry();
    $r_width = $imageprops['width'];
    $r_height = $imageprops['height'];
    if($width > $height){
        $newHeight = $height;
        $newWidth = ($width / $r_height) * $r_width;
    }else{
        $newWidth = $width;
        $newHeight = ($height / $r_width) * $r_height;
    }
    $im->resizeImage($newWidth,$newHeight, imagick::FILTER_LANCZOS, 0.9, true);
    $im->cropImage ($width,$height,0,0);    
    $im->writeImage(APPLICATION_PATH.'/creature_pics/'.$code.'.jpg');
    $im->destroy();

    $newFile = APPLICATION_PATH.'/creature_pics/'.$code.'.jpg';
}

then in your code just add 然后在您的代码中只需添加

if (move_uploaded_file($_FILES["u_c2pic"]["tmp_name"], $target_file2)) {
    //This is the new line calling the function
    $timestamp = microtime(true);
    $newFile = popupImage(600,1200,$timestamp,$_FILES["u_c2pic"]["tmp_name"]);
} else {
    $ercode2 = "Sorry, there was an error uploading your file.";
}

That should create a new file in /creature_pics/ which will be named something like 142023023.9777.jpg which is the timestamp passed into the function as the code. 那应该在/creature_pics/创建一个新文件,其名称将类似于142023023.9777.jpg ,这是作为代码传递给函数的时间戳。

EDIT 编辑

Missed the timestamp element so this is now added in 错过了时间戳元素,因此现在添加了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM