How to add an image from url to temp? - php

I am using the SimpleImage.php class from: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ , to resize, compress and save the images.
Usually using it with an image from input:
$tmp_dir = $_FILES['file']['tmp_name'];
$file_name = 'something.jpg';
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($tmp_dir);
$image->resizeToWidth($width);
$image->save('imgd/l'.$file_name);
But how can I deal just with the image url? (from another website)
$img = file_get_contents($url);
The above $img variable keeps the actual image.
So how can I save it to temp to use it?
If this is the right way.
If it's possible not to have to change SimpleImage.php class.

With tempnam()
Creates a file with a unique filename, with access permission set to
0600, in the specified directory. If the directory does not exist,
tempnam() may generate a file in the system's temporary directory, and
return the name of that.
<?php
$tmpfname = tempnam("/tmp", "UL_IMAGE");
$img = file_get_contents($url);
file_put_contents($tmpfname, $img);
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($tmpfname);
$image->resizeToWidth($width);
$image->save('imgd/l'.$file_name);
?>

Related

Set an extension and make full file from base64 encode to able to use laravel file method in php

I had base64 encoded data.
Look my code, please.
First of all, see my code.
$data = "data:image/png;base64,iVBORw0KGgoAAAA..........";
$image_array_1 = explode(";", $data);
$image_array_2 = explode(",", $image_array_1[1]);
$data = base64_decode($image_array_2[1]);
$imageName = uniqid().time().".png";
I want to set an extension .png to complete my file so that I can count this file extension by laravel method $image->getClientOriginalExtension() and others laravel file methods.
sorry for miss spell of language.
Hope I make you understand.
This works, although I cannot say if it's the best way to go about it. It's a full working example with a 1x1 black pixel png image. This assumes you already removed the data:image/png;base64, portion from the image data.
$data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR
42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=');
// Create a temp file and write the decoded image.
$temp = tmpfile();
fwrite($temp, $data);
// Get the path of the temp file.
$tempPath = stream_get_meta_data($temp)['uri'];
// Initialize the UploadedFile.
$imageName = uniqid().time().".png";
$file = new \Illuminate\Http\UploadedFile($tempPath, $imageName, null, null, true);
// Test if the UploadedFile works normally.
echo $file->getClientOriginalExtension(); // Shows 'png'
$file->storeAs('images', 'test.png'); // Creates image in '\storage\app\images'.
// Delete the temp file.
fclose($temp);

How to store image path in database - using laravel

currently i can store file name is database and in public folder of laravel but i want to store file path in database?
My Controller:
$image=$userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filename]);
How i can store image path in laravel and image name in public folder? i am sending image in json using base64.
Your help will be highly appreciated?
Use:
$image = $userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);
$filePath = Storage::disk('public')->getAdapter()->getPathPrefix();
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filePath.$filename]);
First type this command :
php artisan storage:link
it will create a symlink to public folder.
in your controller, you can write like this :
if ($request->file('image')) {
$file = $request->file('image')->store('images', 'public');
$new_product->image = $file;
}
this code will insert a random string included the path, so you can have something like this on your image column value 'images/shgsqwerfsd.jpg'

Unable to create thumbnail while image is uploading in Laravel

I'm trying to create thumbnail of image while it is uploading. The problem is that the thumbnail isn't created at all. Also is not saved in database.
This is what I have added in my function
$image = $request->file('image');
if( $image && $image->isValid()){
$imagename = str_random(20).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$thumb_img = Image::make($image->getRealPath())->resize(100, 100);
$thumb_img->save($destinationPath.'/'.$imagename,80);
$image->move($destinationPath, $imagename);
}
$item->image = $filename;
$item->image_thumb = $thumb_img;
It's saves only the original image both places - uploads dir and in database but nothing regarding the thumbnail.
I'm using Intervention package.
Nothing is saving because you override the same image twice. Look what you have:
First, you creating thumbnail and saves it into the /uploads
After this, you save the original into the same directory with same name e.g. overriding the thumb.
You just need to make different name for the thumbnail:
$thumb_img = Image::make($image)->resize(100, 100)->save($destinationPath.'/thumb_'.$imagename, 80);
Notice the prefix for the thumb thumb_...

How to encode the image file in PHP

i am using one file upload form field,from that field i want to encode the filename,don't to want to move the any temporary folder,directly i want to insert the database,while display the image directly fetch DB and display the website,I want encode like data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA
$path = basename($_FILES['file']['name']);//here i am getting filename
$im = file_get_contents($path);//here i am getting false
$imdata = base64_encode($im); // so can't here encode the filename
$horoscope = array("encode" => $imdata,"path" =>$path,"getcontents" =>$im);
echo json_encode($horoscope);
PHP uploads the file into a temporary file and tells you where that is in the $_FILES['file']['tmp_name'] the $_FILES['file']['name'] holds the file name that the user called the file on their system, the one they select in the browser.
So if you want to grab the file before you have done a move_uploaded_file() on the file use $_FILES['file']['tmp']
$path = $_FILES['file']['tmp_name'];
$im = file_get_contents($path);
$imdata = base64_encode($im);
$horoscope = array("encode" => $imdata,
"path" =>$path,
"getcontents" =>$im
);
echo json_encode($horoscope);
I was not sure what you were passing back the $path for, so that may still need to be $_FILES['file']['name'].
$horoscope = array("encode" => $imdata,
"path" => $_FILES['file']['name'],
"getcontents" => $im
);
RE:Comment 1
I assume you want to store the base64encoded version of the file to the database so that will be $imdata
RE:Comment 2
To get the extension of the incoming file use pathinfo()
$path = $_FILES['file']['tmp_name'];
$file_parts = pathinfo($path);
$extn = $path_parts['extension'];
But beware, just because the extension says .png is not actual guarantee that it is a .png file

Uploading a base64 image as an image file

So I'm receiving an image in base64 encoding. I want to decode the image and upload it in a specific directory. The encoded file is an image and I'm using $file = base64_decode($base64_string); to decode it. Uploading it via file_put_contents('/uploads/images/', $file); always results in failed to open stream: No such file or directory error.
So, how do I take the decoded string, and upload it on a specific path?
Thanks.
Your path is wrong.
/uploads/images
is checking for a folder in the / directory called uploads. You should specify the full path to the folder:
/var/www/vhosts/MYSITE/uploads/images
I had the same problem and finally solved using this:
<?php
$file = base64_decode($base);
$photo = imagecreatefromstring($file);
//create a random name
$RandomStr = md5(microtime());
$name = substr($RandomStr, 0, 3);
$name .= date('Y-m-d');
$name .= '.jpg';
//assign name and upload your image
imagejpeg($photo, 'images/'.$name, 100);

Categories