Keeping uploaded images in tmpfolder for resizing - php

I am working on an upload script that also resizes/rescales an image.
Currently it is working by uploading the image, moving to the upload dir (site.com/upload) and then resizing, and afterwards deleting the original again...
Now my question is:
Can I do this without moving the original to the upload dir and even better, also keeping the new file in tmp so I the user can afterwards confirm the image (so If they don't want it and just hit the 'back' button it won't stay in the upload dir.)
Current code:
move_uploaded_file($_FILES['file']['tmp_name'], 'resize-upload/'.$_FILES['file']['name']);
$filename=$_FILES['file']['name'];
$Imagick=new Imagick();
$Imagick-> readImage('resize-upload/'.$filename);
$Imagick-> scaleImage(200,200,auto);
$Imagick-> writeImage('resize-upload/resized-'.$filename);
unlink('resize-upload/'.$filename);

You can't. You don't have the access to the image if you haven't moved it yet. But you can generate more than one thumbnail at a time from same image/object and if you are storing/keeping the original also then you don't need to unlink it either.
Another way would be to use flash and resize the image on the client side and then only save the resized image.

Related

Original path for PHP image upload

I was looking for a good image upload plugin for CakePHP2 and found these ones:
https://github.com/angelitomg/QimageComponent
https://github.com/BradCrumb/CakeImageCropResize (my preference)
Both image uploads needs a $imagePath and a potential options array. Am I right that $imagePath should always be a path to an image on the server? Why can't it be the temporary image path (e.g. C:\xampp\tmp\php8454.tmp)?

Overwrite image in php while uploading

I want to know that how can I overwrite images when they uploaded to server in php. For example I uploaded a photo to a folder as soon as I upload another image it will take place of previous image. Image name is not same it may differ. Thanks
Delete the previous image before uploading the new one using:
$path = $_SERVER['DOCUMENT_ROOT'].'img/img.jpg';
unlink($path);
This code basically assigns the path of the image to $path and deletes the image using unlink($path);

$_FILES['file']['tmp_name'] + Cropping from the tmp Image

I'm currently allowing users to upload an img and then I save the image as folows:
$tmp = imagecreatetruecolor($configWidth, $configHeight);
imagecopyresampled($tmp, $src, 0,0,0,0,$configWidth,$configHeight,$origWidth,$origHeight);
imagejpeg($tmp, $_SERVER['DOCUMENT_ROOT'].$folder.$newFileName,90);
I then show the image to the user so they can crop the image.
I wanted to ask if I could display the image for cropping without saving it - eg: could I show the image to the user for cropping from the tmp_name - the tmp php image?
I'm not sure if this is possible but it would avoid having to save an image that is then later deleted post cropping...
thx
could I show the image to the user for cropping from the tmp_name - the tmp php image?
No the image is outside the webroot also the image will be deleted when the upload script finishes execution.
With some html5 methods you can show an image on a page without uploading it. see https://developer.mozilla.org/en-US/docs/DOM/FileReader#Example

PHP set an image on my server as uploaded temp file

I am editing a photo gallery script to allow the use of TIFF to be uploaded and saved, but i must keep the files in jpg format also for web viewing.
What I have done is installed image magick to convert TIF to JPEG, once i have it converted I want the script to continue with making thumbnails, zoom images, etc. it makes them from
$_FILES['image']['tmp_name']
Is there a way to set my newly created file as $_FILES['image']['tmp_name']? my new jpeg file path is set to $nw.
basically I need
$nw='path/to/newfile.jpg';
$_FILES['image']['tmp_name']=$nw;
but it does not work. any ideas?
If you need to work on the same file across multiple page requests, move it somewhere safe using move_uploaded_file.
If the functions that you wrote require access to $_FILES['image']['tmp_name'], rewrite them to accept the name of the file as a parameter and call them using the new location of the file as argument.

How can I resize an image already uploaded using Magickwand(PHP)/ImageMagick?

I am working on building gallery where the user uploads all the images. I had tried to use GD originally but found that it used way too much memory when dealing with images from a digital camera. So I have been looking into ImageMagick and ran into this problem.
My end goal is to resize the image and then upload it. I am not sure if this is possible with ImageMagick or not. I have gotten it to resize the image after upload but it doesn't save the resized image, just the original size.
This is the code I am currently using: ($image is the path to the file on my server)
$resource = NewMagickWand();
MagickReadImage($resource,$image);
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500');
Any input would be appreciated,
Levi
Your code will send the modified image to the client (the web browser), but it will not save it to the server (replacing the original image, for example)
To save the image, use:
MagickWriteImage( $resource, 'new_image.jpg' );

Categories