I'm developing a site where visitors can upload their own property images. No problem with the upload part, but the problem is with resizing. Firstly, is it a good idea to resize at the server (the alternative being an instruction to the visitor on how to prepare their own images) anyway?
Here is my uploader.php file which I want to resize an image to a maximum of 400px width or height:
Code: Select all
<?php
$listingid = $_POST['listing'];
$filename = $_FILES['file']['name'];
// Set a maximum height and width
$width = 400;
$height = 400;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
if($_FILES['file']['name'] !="")
{
copy ($_FILES['file']['tmp_name'],
"/properties/mainimg".$listingid.".jpg")
or die("Could not copy file");
header("location:get_listings.php");
exit;
}
else{ die("No file specified");}
?>
Many thanks