PHP Wait For Image Upload to Finish Before Resizing - php

Is there a way to "pause" php to wait for a file to upload before manipulating it?
I am using this to upload photos:
move_uploaded_file($_FILES[$PhotoField]["tmp_name"], $BrandDirectory . "/" . $NewPhotoName);
That part seems to be working well. The issue is that after that I am resizing the photo that is uploaded using:
$image = new SimpleImage();
$image->load($BrandDirectory . "/" . $NewPhotoName);
$image->resizeToWidth(1024);
$image->save($BrandDirectory . "/" . $NewPhotoName);
That part seems to work as well to, as long as the photo has finished uploading.
So, it seems that I need some way for everything to wait before the upload finishes.

if(move_uploaded_file($_FILES[$PhotoField]["tmp_name"], $BrandDirectory . "/" . $NewPhotoName)){
$image = new SimpleImage();
$image->load($BrandDirectory . "/" . $NewPhotoName);
$image->resizeToWidth(1024);
$image->save($BrandDirectory . "/" . $NewPhotoName);
}

Related

How to duplicate the uploaded GIF and change its size?

Can anyone suggest a solution to the issue?
There is such a code
private function saveGif() {
$path = PATH_TEMP . '/' . $this->post('hash') . '.gif';
$this->web->Request($this->post('external'), null, true)->SaveToFile($path);
}
private function saveThumbs() {
$path = PATH_TEMP . '/' . $this->post('hash');
$img = new Image($path . '.gif');
$img->toFile($path . '.jpg', 'image/jpeg');
$img->resize(350, null)->toFile(PATH_TEMP . '/m_' . $this->post('hash') .'.jpg', 'image/jpeg');
}
If you add
$img->resize(250, null)->toFile(PATH_TEMP . '/m_' . $this->post('hash') .'.gif', 'image/gif');
Then a file m_...gif of the desired size is created, but there is no animation in the gif.
Maybe of course I'm not looking there, but still.
Sorry if I'm not writing correctly.
It is necessary that when uploading a gif to the site, the file appears m_...gif, with the width I need.

Reduce image size in laravel

I used ImageOptimizer package for reducing image size.
source: http://image.intervention.io/getting_started/installation
in controller
use Image;
if (Input::hasFile('title_image')) {
/*$this->validate($request,[
'photo' =>'required|image|mimes:jpg,jpeg,png|max:2048'
]);*/
$Product = Input::file('title_image');
$Product->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
Now I want to convert image in this function when I upload. if I add this method $img = Image::make('foo.jpg')->resize(300, 200); it says storage not found error.
Now what can I do. please give me some suggestion. Thanks in advance.
Please use "resize function" before move into desire folder.
use Image;
if (Input::hasFile('title_image')) {
/*$this->validate($request,[
'photo' =>'required|image|mimes:jpg,jpeg,png|max:2048'
]);*/
$Product = Input::file('title_image');
$filename = time() . '.' . $Product->getClientOriginalExtension();
Image::make($Product)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) )->move(public_path() . '/../../products', md5($Product->getClientOriginalName()) . ".png");
$product->title_img = "products/" . md5($Product->getClientOriginalName()) . ".png";
}
if you want reduce or resize the image you can use the laravel image compression package before upload the image.this is link below how you can do that.
https://www.itsolutionstuff.com/post/laravel-compress-image-before-upload-exampleexample.html
image compression is need more because when browser access the url and that page having the more that 10 image then it first download the all image and send request to server to for each due to which its taking more time to load the page.so,get ride from this problem use image compression package.

Laravel move() is not uploading images everytime

I use code below:
$image_name = 'image' . time() . '.' . $request->file('image')->getClientOriginalExtension();
$destinationFolder = public_path('images');
$request->file('image')->move($destinationFolder, $image_name);
but sometime its not working, images are not storing. Im using heroku as host.
Try to use Storage facade :
$path = \Storage::putFile('images', $request->file('image'));
Laravel will generate a name automatically, About Storage facade, just see this. :)
I made function for upload file and if there is no uploaded file do it again till its upload
$imageFile->move($destinationFolder, $fileName);
if(file_exists(public_path('images') . '/' . $fileName))
return public_path('images') . '/' . $fileName;
else
self::uploadImage($imageFile, $fileName);
I used the code and it's working fine.
self::$image = $request->file('image');
self::$imageName = self::$image->getClientOriginalName();
self::$directory = 'category-images/';
self::$image->move(self::$directory, self::$imageName);
return self::$directory.self::$imageName;

Image creation from pdf

I am creating a cron job in which I copy files from one directroy to another. The cron job works fine it copies the files to the import directory. The structure of import directory is like this.
import
folder1
pdf_file1
folder2
pdf_file2
I am trying to create a thumbnail image for each pdf file that is copied to the folders, I installed ImageMagik and my php code to create a thumnail image is
if ($preview != "") {
$copy_res = ftp_get("files/upload/" . $ftp_upload["path"] . "/" . $preview, "files/import/" . $ftp_upload["preview"] . "/" . $preview);
$md5_checksum = md5("files/import/" . $ftp_upload["path"] . "/" . $preview);
} else {
//$pdf_file = 'files/import/folder1/pdf_file1';
$pdf_file = 'files/import/' . $ftp_upload["path"]."/".$filename_new;
if (file_exists($pdf_file)){
echo 'I am here';
exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
}
}
When I run the code it comes to the else statement and echo the line but its not executing the exec command.
I checked the convert command from Command Prompt for that I navigated to
C:\wamp\www\project\files\import\folder1
and then I run this command
exec("convert -density 300 test.pdf[0] test.jpg");
From the command prompt it worked but in my code it s not working .
Is there any issue with the path ? because the file is alreday copied when I try to create the thumbnail for it.
The ImageMagik is installed in
C:\Program Files(x86)\ImageMagik
Any ideas what I am doing wron ? and is there any other faster way to create thumbail image for pdf ?
Thanks in advance
I now write my code seperating the code form the exec() so that you can display the information you are sending to Imagemagick.
I would also try setting the filenames outside the exec()
$input = $pdf_file . "[0]";
$output = $filename_new . ".jpg"
$cmd = " -density 300 $input -thumbnail 200x300 $output ";
// echo $cmd."<br>";
exec("convert $cmd");
Added a resize but it will keep the image proportions - if you need an exact size you could crop the image or pad it with a colour.
There seems to be a missing space that separates the path to the pdf file and the new name for the image file.
exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
Should be:
exec("convert -density 300 " . $pdf_file . "[0] " . $filename_new . ".jpg");
-----------------------------------------------^

How to make the thumbnail go to /tmp/thumb/ and keep the original image on /tmp/ with Image Resizer?

I'm trying to play with image resizer and I got the following code
if (is_uploaded_file(#$_FILES['ulimage']['tmp_name'])){
$targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . #$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/" . $targetfilename, $width, $height);
}
For now, the original image and the thumbnail will place in same folder.
Let me know..
Source
Well, the answer is :
if (is_uploaded_file(#$_FILES['ulimage']['tmp_name']))
{
$targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . #$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
}
But maybe you'd like to understand a bit the code you copy and past from the Net before using it. Using $_ vars without escaping system and with # to hide error is not really calling for trust...
EDIT : I'm giving advices, but maybe it's better to give some explanation as well.
// first you check if the is done uploading in the tmp directory with is tmp name
if (is_uploaded_file(#$_FILES['ulimage']['tmp_name']))
{
// here, you rebuild a explicit name using the original filename and a
// unique ID to avoid erasing another one
$targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
// you rename the file an put it in ./tmp, a subdir of the
// script file (because of dirname(__FILE__))
move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
// Here create a rezided copy
// so it's here you can decide to make it go to ./tmp/thumb
// make sure the dir exists before because you have no clue here
// if ImageHelper will create it for you if not
ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/thumb/" . #$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
}
Hey man, this looks very tough, but can be made simply using the Thumbnailer library and its upload helper:
function callback(& $thumb) {
$thumb->thumbSquare(100)->save("/tmp/thumb/".$thumb->filename);
}
Thumbnailer::upload('ulimage', 'callback');
Very easy :)

Categories