S3 creates blank transparent webp image - php

I have uploaded webp image into Amazon-S3. But sometimes it generates a blank transparent webp image. Content-Type:image/webp and file-size are also correct. Also, note that it displays from the mobile application but not in any browser. All is working fine on the local server. Problems occur in only with the live server. I've used the following code:
$disk = Storage::disk('s3');
$original_targetFile = "videoflyer/webp_original/" . $image;
$disk->put($original_targetFile, file_get_contents($original_sourceFile),
'public');
S3 generates a blank image like this:
https://imgur.com/a/CMx6tsU

Sorry, it's my miss understanding. There is an issue of GD driver.
Now I am using https://developers.google.com/speed/webp/ & it's work perfectly.

Related

Convert .HEIC to .JPG with ImageMagick in PHP

I am looking to try and create a small image converter that would convert HEIC files that are uploaded to a php web document to .JPG (or any other generic file format).
I am running PHP off a unix server and have ImageMagick installed on the server. The following command line code works from the server:
mogrify -format jpg *.HEIC
I'd like to convert this command line code to PHP.
As mentioned I like to convert the command line formatting code to PHP. I currently have the following code set up in a basic HTML + PHP form. The file being converted is newly uploaded and not located on the server. If necessary I can upload to the server first then read form the server file.
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_FILES['image_url']['name']))
{
echo "No File uploaded";
}
else{
$uploadedImage = fopen($_FILES['image_url']['tmp_name'], 'rb');
$image_to_convert = new Imagick();
$image_to_convert->readImageFile($uploadedImage);
$image_to_convert->setFormat("jpg");
$image_to_convert->setFileName('test.jpg');
header('Content-Type: image/jpg');
header('Content-disposition: attachment; filename='.$image_to_convert->getFileName());
header("Content-Description: File Transfer");
readfile($image_to_convert);
}
}
This code downloads a "test.jpg" file, but when I try to open it in Windows image viewer it displays a "It looks like we don't support this file format" message. I'm relatively new to PHP so I don't know all the tricks for output/input streams so if my code is horrible let me know.
Any and all help is welcome. Thanks!
I think you need to specify 'jpeg' rather than 'jpg' for your format.
$image_to_convert->setFormat("jpeg");
Managed to get it working after I changed from image formats like png and jpeg, and instead downloaded the converted HEIC image as a .PDF file.
I also had to upload (write) the PDF file to the server first before I was able to download (read) it through the website.

Convert image from png to webp then upload with ftp in Laravel

Preface
Environment
OS: Ubuntu
PHP: 7.4
Laravel: ^8.12
I'm writing a scraper page for a web app that I'm developing and these are the steps that I'm trying to achieve:
Scrape image from the target website.
Convert image from png to webp
Upload converted webp to my server via FTP.
The core of the program is one line:
Storage::disk('ftp')->put("/FILE_UPLOAD_LOCATION", file_get_contents("scraped-image.png"));
This confirmed that my FTP environment was configured correctly.
Attempts and Errors
I should just be able to do the following should I not?
$image = createimagefromstring(file_get_contents("remote_url.png");
imagepalettetotruecolor($image); // needed for webp
Storage::disk('ftp')->put('remote_desination', imagewebp($image));
The answer is no. This does not work as it just creates a "image" file with 1 as the contents of it.
I haven't tried much outside of this, but hopefully someone has an answer or can point me in a new direction.
This is happening because imagewebp() doesn't return the image, but a boolean indicating either it worked or not.
You must create a handle to store the image in memory then use it to store on the ftp:
$handle=fopen("php://memory", "rw");
$image = createimagefromstring(file_get_contents("remote_url.png");
imagepalettetotruecolor($image); // needed for webp
imagewebp($image, $handle);
imagedestroy($image); // free up memory
rewind($handle); // not sure if it's necessary
Storage::disk('ftp')->put('remote_desination', $handle);
fclose($handle); // close the handle and free up memory

FTP image upload doesn't work properly in PHP

I have a web application which is connected to a website. my web application runs in a Ubuntu server(LAMP). in my web application, there is a function called add a new client. when adding a new client user have to set an image for the client. when a user clicks the save button, the image should upload to the Linux server itself and I'm sending the image to my shared hosting server where the website is hosted using FTP. This perfectly works in my windows localhost (I'm using wamp server). but when I put my web application to the Ubuntu server, FTP image upload works sometimes and sometimes it doesn't work.
this is my code
private $config_ftp = array('hostname'=>'xxxx.com','username'=>'xxx#xxxxx.com','password'=>'xxxxx','passive'=>FALSE,'debug'=>FALSE,'port'=>21);
$source_original = $this->img_path['img_original'] . $file_name;
$destination_original = $this->full_file_path['img_original'] . $file_name;
if($this->ftp->upload($source_original,"./assets".$destination_original)) {
$this->response['msg']="Images Uploaded successfully!";
$this->response['status']=true;
}
when I check the destination folder after the upload it looks like this. The file size is 0kb. sometimes it gets uploaded properly. and I have to mention again this process works completely fine in windows localhost.
Thanks.
$this->ftp->upload() code is need to be shown to find actual problem.
in that if $image_size = $_FILES['image']['size']; $image_size is 0 that you have problem in image otherwise you can successfully upload it by move_uploaded_file($_FILES['image']['tmp_name'], "YOUR-UPLOAD-FOLDER/YOUR-IMAGE-NAME-PLUS-EXTENSION"); .
similar problem : 0 kb when upload and store image to server
The issue was there in my ftp_config.
what i had to do is set passive and debug as true
private $config_ftp = array('hostname'=>'xxxx.com','username'=>'xxx#xxxxx.com','password'=>'xxxxx','passive'=>TRUE,'debug'=>TRUE ,'port'=>21);

Replace image on server and show previous image until the new one is fully uploaded

I'm uploading to a lightspeed server through "ncftpput" an image taken from a raspberry camera every minute.
I want to be able to show the updated image and I know how I can force the browser to use the latest version instead of the cached image.
So, everything works properly except that, if I refresh the image (like through shift-F5) during image upload, the browser reports image contains errors (or shows image partially).
Is there any way to ensure the image is fully loaded before serving the new one?
I'm not sure if I should operate on ncftp or use PHP to ensure the swap happens only after complete loading.
Image is a progressive jpg but that doesn't help...
Any suggestion?
Thanks
I ended up with NOT using FTP because, as Viney mentioned, the webserver doesn't know if the upload is completed.
I'm using "curl" which has the advantage of being preinstalled on raspberry distro and a php upload page.
It seems that PHP will pass the new image only once fully uploaded and avoid creating the issue when image is still partially uploaded.
So, to recap:
raspberry (webcam), after having taken the image:
curl -F"somepostparam=abcd" -F"operation=upload" -F"file=#filename.jpg" https://www.myserver.com/upload.php
PHP server code:
$uploadfile = '/home/domain/myserver.com/' . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
$content = file_get_contents($uploadfile);
Problem is this: you open browser (at 07:00:10 AM) image.jpg gets rendered now say it's 07:01:00 you hit refresh in browser but the raspberry is already started uploading image.jpg say it would take 3 secs to complete the full upload but the server doesn't know about ftp and would read whatever bytes are there in image,jpg and flushes to your browser. Had it been a baseline JPG it would have shown a cropped(in height) image but since it's a progressive JPG it would be messed up.I am not aware of whether it's possible but try looking up if you FTP supports a locking file.
How to solve ?
The best way is to let you server know that the file it's accessing is in a write process.If your ftp supports advisory locks then you can use it so when server tries accessing you file ( via system call) kernel would instruct it that the file is currently locked so the server will wait until ftp releases that lock.
In vsftp there would be a option lock_upload_files in VSFTPD.CONF setting yest would enable this feature
If you are unable to work out above solution then you can use some trick like checking file last modified time if it's almost same as current time then you make php wait for some guessed time that you think is avg. upload time of your file.For this method you should use PHP to send it to browser instead of server. ie Just change the src of your image from '/path/to/image.jpg' to 'gen-image.php'. This script will read that image and would flush it to the browser
gen-image.php
$guessedUploadTime = 3;// guessed time that ncftpput takes to finish
$currTime = time();
$modTime = filemtime('image.jpg');
if( ($currTime - $modTime) < $guessedUploadTime)
{
sleep($guessedUploadTime);
}
$file = '/path/to/image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
readfile($file);
Note that the above solution is not ideal because if file has been just done uploading it won't be modified for another 57 seconds yet the browser request say at 07:02:04 has to wait unnecessarily for 3 seconds because mtime would be 07:02:03 and browser would get file only after 07:02:06. I would recommend you to search for some way(proly cmd based) to make server and ftp go hand in hand one should know the status of the other because that is the root cause of this problem.

PHP thumbnail generator not processing thumbnails

I am using a PHP Thumbnail Generating script that I have used without issue many times in the past. I am currently using it without issue on the very same domain that I am now having problems.
The script is processing to a point. It is generating physical thumbnail images and saving them into the preset cache directory but it is not returning resized images to the browser.
The image src is in the following format:
<img src="thumbnail.php?file=sample/02.jpg&w=100&h=70&el=0&gd=0" />
I have confirmed that the image exists and can be displayed in a browser itself.
Also, the PHP script loads ok as I have tested it by adding an 'echo' to confirm.
I have installed this script in multiple locations on the server and the result is the same. I have even tried other versions of PHP Thumbnail Generators and none of them produce resized images in the browsers.
I have checked that GD is active on my server:
GD Support enabled
GD Version bundled (2.1.0 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.4.2
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 6b
PNG Support enabled
libPNG Version 1.2.44
WBMP Support enabled
XBM Support enabled
PHP is version 5.3.29
Imagemagick is available and path confirmed as '/usr/bin/convert/'.
Here you see the result:
http://demos.mitey.co.nz/image_processor/index.php
All that appears is broken image placeholders instead of the dynamically generated thumbnails.
All relevant permissions have be set to 0777 or 0755, tried both.
This really isn't a difficult script to use and I have numerous times in the past but I am unable to debug whatever is causing it to fail in this instance.
Anybody have any ideas as to what could be stopping the image source from correctly processing the PHP contained within it?
I have tried so many different things that I can no longer focus on the issue and come up with anything else to try.
SOLVED: I was trying to load images into the script from another sub-domain. The Thumbnail script I was using claims to allow loading images from another domain using http however it thens fails when it comes to the PHP function 'file_exists' which does not work via the http protocol. I removed the 'http://' part of the image path using 'str_replace' and replaced it with the file system path, i.e. /home/siteroot/' and all works as expected. When used with images on the same domain as the thumbnail generating script the script is happy for the full 'http://' path to remain. There was no useful error messages to point this out.
Download the latest PHPthumb from website
For generate thumb see documentation
don't forget to Change variable as per your need.
$thumbUrl = "somefolder/new.jpg";
$resizeWidth = "70"; // in pixels
$resizeHeight = "70"; // in pixels
$imageThumb = $directoryPath. "/phpThumb/phpThumb.php?src=" . $thumb1 . "&w=".$resizeWidth."&h=".$resizeHeight."&iar=1&hash=90e9d46c4d138a72574c52e31fbef0dc";
echo '<img src="'.$imageThumb.'" alt="filename.jpg" />';

Categories