I use a super simple image resize php script, which I have integrated on one of my sites based on wordpress. I allow people to resize their image one-by-one, and basically after the resize is done I display it to them in the browser so they could download/save. Here goes the html part.
<form action="http://awesomeness.com/wp-content/themes/awesome/resize/processupload.php" method="post" enctype="multipart/form-data" id="UploadForm">
<input name="ImageFile" type="file" />
<input type="submit" id="SubmitButton" value="Upload" />
</form>
<div id="output"></div>
There is also a little jquery script that I think is pointless to show here. Its only function is put the resized image into the output-div
below the form. Below goes the contents of the mentioned processupload.php.
if(isset($_POST)) {
$BigImageMaxSize = 800;
$DestinationDirectory = 'uploads/';
$Quality = 90;
if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])) {
die('Something went wrong with Upload!');
}
$RandomNumber = uniqid();
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
$ImageSize = $_FILES['ImageFile']['size'];
$TempSrc = $_FILES['ImageFile']['tmp_name'];
$ImageType = $_FILES['ImageFile']['type'];
switch(strtolower($ImageType))
{
case 'image/png':
$CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
break;
default:
die('Unsupported File!'); //output error and exit
}
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt;
$DestRandImageName = $DestinationDirectory.$NewImageName;
if(resize_Image($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType)) {
echo '<div id="output">';
echo '<img src="http://awesomewebsite.com/uploads/'.$NewImageName.'" alt="Resized">';
echo '</div>';
} else {
die('Resize Error'); //output error
}
}
function resize_Image($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType) {
if($CurWidth <= 0 || $CurHeight <= 0) {
return false;
}
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//delete original image and free some memory
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
You can see that the original image is deleted automatically by the script once the resize takes place. But I need the resized image deleted from my /uploads
folder too. Basically I just want the script to resize the image and display the resized image on the browser itself only, not to save it on server anywhere. Is it possible. If it is could you please suggest me the edits or edit the code in question itself.Credit for original script.
Bottomline of the question would be : How can this script display the resized image on the visitor's browser without saving any file ( original or resized ) anywhere on server.
THINGS TRIED : I am still trying to decipher this somewhat similar thread but still am clueless. I am going to keep updating this space until this thing is over. I am trying unlink() method now but I am unable to find the variable to unlink, is it $DestRandImageName
? coz unlink($DestRandImageName)
that does not seem to work.
FINAL UPDATE : After countless minor efforts I finally got it done. I dont think I should add it as answer so, here. I got all my solutions in this tutorial. Discovered that if I only need to reproduce the image to the browser without saving it, I will need to take a different approach employing php functions like imagecopyresampled() and imagejpeg(). Learned it the hard way, but glad.