convert ico to png formatter in vue laravel - php

i am trying to convert ico format file to png format
$media_url ="https://static.licdn.com/scds/common/u/images/logos/favicons/v1/favicon.ico"
$filePath = public_path('media_downloads').'/pnff.png';
file_put_contents($filePath, file_get_contents($media_url));
$s3 = \Storage::disk('s3');
$res = $s3->put($filePath, file_get_contents($img), 'public');
$downloadUrl = \Storage::disk('s3')->url($filePath);
i got s3 url but the file is not opening i think it is corrupted how can i change the image content type while saving to a folder also in s3

i have done ico to png convertion by using Imagick Thanks #aynber #Chris Haas for the guidance
use Imagick;
$file = $media_url;
$extension = pathinfo(parse_url($file, PHP_URL_PATH), PATHINFO_EXTENSION);
$filename = 'linkedin_thumbnail' . time() . '.' . $extension;
$local_file_path = public_path('media_downloads') . '/' . $filename;
$contents = file_get_contents($file);
file_put_contents($local_file_path, $contents);
$image = new Imagick();
$image->readImage($local_file_path);
$image->cropThumbnailImage(100,100);
$image->setImageFormat("png");
header("Content-type: image/png");
$filePath2 = public_path('media_downloads').'/'.date('ymdhis').'.png';
$image->writeImage( $filePath2 );
$media_url = $filePath2;
unlink($local_file_path);
unlink($filePath2);

Related

How to resize image before upload in laravel?

I want to resize image beofre upload it into database. I store image in this way
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$success = $image->move($upload_path1, $image_full_name);
}
Now, I want to resize image before uploading it. I try to use intervation package, I try this
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$img = Image::make($image)->resize(300, 200);
$img->save($upload_path1, 60, $image_full_name);
}
and I got this error
"Encoding format (1691942233153059.jpg) is not supported."
What is the best way to resize image before upload and also set image with unique name?
1- use getRealPath() inside Image::make()
2- save image in particular path.
if($request->hasFile('image')) {
$image = $request->file('image');
$file_name = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$file_name));
}

Failed to rename image and upload using storeAs in Laravel

I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //

How do I use Image Intervention with Laravel Storage and digital ocean

I am using the Laravel storage feature to store images on DigitalOcean which works fine but I can't seem to add image intervention because I want to use the orientate feature. This is so that images created through the phone doesn't rotate sideways. How do I add image intervention to my existing code?
if ( $request->hasFile('image')) {
$extension = $request->file('image')->extension();
$mimeType = $request->file('image')->getMimeType();
$path = Storage::disk('do')->putFileAs('public/images', $request->file('image'), time() . '.' . $extension);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;
}
So i tried different ways of implementing image intervention haven't had much success but here my updated code. An with that i got a error saying Command (GetRealPath) is not available for driver (Gd).
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('/images/' . $filename);
$extension = $request->file('image')->extension();
//$mimeType = $request->file('image')->getMimeType();
$orientate = Image::make($image)->orientate();
$path = Storage::disk('do')->putFileAs('public/images', $orientate, $filename);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;

Decoding base64 image and uploading it to server with php

I'm passing image as a base64 string from a form. Then I would like to decode it, rename it and upload it to my server folder. I can't get this to work so obviously I'm doing something wrong here. Any help would be appreciated.
My code:
$image = $_POST['image-data'];//base64 string of a .jpg image passed from form:
$image = str_replace('data:image/png;base64,', '', $image);//Getting rid of the start of the string:
$image = str_replace(' ', '+', $image);
$image = base64_decode($image);
$ext = strtolower(substr(strrchr($image, '.'), 1)); //Get extension of image
$lenght = 10;
$image_name = substr(str_shuffle("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ"), 0, $lenght);
$image_name = $image_name . '.' . $ext; //New image name
$uploaddir = '/var/www/vhosts/mydomain.com/httpdocs/img/'; //Upload image to server
$uploadfile = $uploaddir . $image_name;
move_uploaded_file('$image', $uploadfile);
Using file put content you can move files to folder
$file = $_POST['image-data'];
$pos = strpos($file, ';');
$type = explode(':', substr($file, 0, $pos))[1];
$mime = explode('/', $type);
$pathImage = "path to move file/".time().$mime[1];
$file = substr($file, strpos($file, ',') + 1, strlen($file));
$dataBase64 = base64_decode($file);
file_put_contents($pathImage, $dataBase64);

Adding random number to uploaded file in PHP

I have my upload php code where my intent is , obtained file from $_files,
add a random number between 0 and 9999 to the name of image like this:
image sent : image.jpg
before saving : image321.jpg
the image is saved in my upload folder but the filename are like
"php2983204tmp"
if ($file !== null) {
$rand = rand(0000,9999);
$path = "some_path";
$file_name = $file->getClientOriginalName(); // file
$extension = $file->getClientOriginalExtension(); // jpg
$file->move($path, $file_name.$rand.$extension);
$response = "File loaded successfully: " . $file_name.$extension;
$response .= '<br>size: ' . filesize($path . '/' . $file->getClientOriginalName()) / 1024 . ' kb';
return new Response($response);
any ideas to fix?
The filename in your example is php and your extension is tmp. None of them have the . that you are missing.
You need to add the dot . as a string after the $file_name and $rand, before the $extension like this:
$file->move($path, $file_name.$rand. "." .$extension);
TIME is always unique identity, use it as below (maybe helpful):
if ($file !== null) {
$rand = rand(0000,9999).time();
$path = "some_path";
$file_name = $file->getClientOriginalName(); // file
$extension = $file->getClientOriginalExtension(); // jpg
$file->move($path, $file_name.$rand.$extension);
$response = "File loaded successfully: " . $file_name.$extension;
$response .= '<br>size: ' . filesize($path . '/' . $file->getClientOriginalName()) / 1024 . ' kb';
return new Response($response);
You need add in the desired chars to the actual string.
$file->move($path, $file_name.$rand.".".$extension);
But I have to say, I am against how you've done this, you don't even check if the "newly" created string already exists in the directory. Its better to hash the time of upload with the original filename, rename the file to the new hash and use a database to point to the file as this way the filename collisions don't occur.
$fn = md5(microtime(true) . $extension . $file_name);
$file->move($path, $fn);

Categories