0

What code would I use to:

  1. Resize all images being uploaded to 600px width while maintaining aspect ratio for height
  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.

Emissary
  • 9,954
  • 8
  • 54
  • 65
nope
  • 1
  • 1
  • change this line: `$target_file2 = $target_dir2 . basename($_FILES["u_c2pic"]["name"]);` to `$target_file2 = $target_dir2.'new_name.extension';` – RNK Mar 24 '15 at 14:32
  • Try this? http://stackoverflow.com/questions/18805497/php-resize-image-on-upload/40324941#40324941 – HoldOffHunger Oct 31 '16 at 00:19

1 Answers1

0

If you can use Imagick on your server then the following code should help you out.

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.

EDIT

Missed the timestamp element so this is now added in

Justin Erswell
  • 688
  • 7
  • 42
  • 87
  • is there an easier way than this at all? I've only really done design stuff before not proper code so don't really understand what most stuff does/means – nope Mar 25 '15 at 18:00
  • You could use a framework to do this but that would involve classes which is what you wanted to avoid. The code is relatively simple, it leverages the Imagick library to get the image dimensions ```(width & height)``` then it checks to see if the image is wider than it is tall (portrait) and aplys the new width & height setting a high number to the long edge to ensure aspect ratio, then again using Imagick, the code resizes and crops and saves a new version by writing it to your folder with the timestamp. – Justin Erswell Mar 27 '15 at 09:15