Saving PHP images

Asked

Viewed 37 times

1

I have a little code that picks up the image, gives a name random for it and saved in a folder. But now I need that in that same code generates the same image but with the size 100x100 and save in the same folder with the same name, only adding at the end "_small". How can I do this?

   <?php
error_reporting(E_ALL);

$file = file_get_contents("php://input");
$id = $_GET['photoId'];

if(strlen($file) < 50) {
    die("2");
}

$imageName = substr(md5(time() . rand(1, 9999)), 0, 24);
file_put_contents("../photo/" . $imageName . ".png", $file);

echo $imageName;

?>

1 answer

2


Create a function to resize the image using Imagemagick or GD. Then make a copy of the file and resize.

 <?php
error_reporting(E_ALL);

$file = file_get_contents("php://input");
$id = $_GET['photoId'];

if(strlen($file) < 50) {
    die("2");
}

$imageName = substr(md5(time() . rand(1, 9999)), 0, 24);

function resize_image($file, $w, $h) {
   list($width, $height) = getimagesize($file);
   $src = imagecreatefrompng($file);
   $dst = imagecreatetruecolor($w, $h);
   imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
   return $dst;
}


file_put_contents("../photo/" . $imageName . ".png", $file);

$photo = "../photo/" . $imageName . ".png";
$photosmall = "../photo/" . $imageName . "_small" . ".png";

if (!copy($photo, $photosmall)) {
    echo "falhou ao copiar $photo...\n";
}

$imgsmall = resize_image($photosmall, 100, 100);

echo $imageName;

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.