1

Following is the code snippet -

list($campaign_image_width, $campaign_image_height, $campaign_image_type, $campaign_image_attr)=getimagesize($campaign_image);

Wherein $campaign_image contains the url of third party images.

Problem

$campaign_image_width comes out empty for this url -

https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw

I am not sure if it is the limitation of getimagesize(), because of unsupported format, which is causing this, or it is because of accessibility issues with the image.

Note -

- The =h256-rw appeneded at the end seems to tell the server to return a different sized version of the image.

- I found that if I try to open the file using firefox browser, it does not display the image, but rather asks to download a webp file (an image format by google it seems). Google chrome opens the file and displays the image, normally.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

2 Answers2

4

Since your server is already downloading the file you might as well do it yourself (if the problem is that it can't do it correctly for webp). You can easily do this using the GD methods imagecreatefromwebp with imagesx and imagesy:

<?php

$url = 'https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw';

$img = imagecreatefromwebp($url);

$width = imagesx($img);
$height = imagesy($img);

var_dump($width, $height);

Note: imagecreatefromwebp() was first introduced in PHP 5.5, so make sure your minimum version is 5.5 with the GD extension installed.

If possible you can install Google's own webp converter as a binary on your server:

https://developers.google.com/speed/webp/docs/compiling#building

In this instance you're running Amazon linux which is based on Fedora and hence uses yum as the package manager, so you should be able to run the following command:

sudo yum install libwebp;

Once you have installed this you can make sure that your safemode supports the binary by using safe_mode_exec_dir and one of the following execution methods:

Once you've run the conversion to eg. JPG, you can run the usual PHP tools to get the image dimensions:

$hnd = imagecreatefromjpeg('convertedImage.jpg');

$width = imagesx($hnd);
$height = imagesy($hnd);
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • Too old version :(, cannot follow this – Sandeepan Nath Mar 07 '16 at 12:17
  • 1
    @SandeepanNath Then I'm afraid you're out of luck GD wise, as it was first added then. If you have access to CLI tools you [can use Google's own converter](http://stackoverflow.com/questions/25248382/how-to-create-a-webp-image-in-php). – h2ooooooo Mar 07 '16 at 12:21
  • Thanks. Could you provide some links to do the setup on linux? I went through https://developers.google.com/speed/webp/docs/compiling, but am not sure which components do I need. Do I need to install the libjpeg, libpng, libtiff and libgif packages? I guess I do not need to convert to/from those formats. I just need to resize the source webp file. – Sandeepan Nath Mar 07 '16 at 12:54
  • @SandeepanNath "linux" is a very wide thing. Do you use Ubuntu? Debian? CentOS? Redhat? Arch? SUSE? – h2ooooooo Mar 07 '16 at 12:55
  • Oops.. Ours is Amazon linux. – Sandeepan Nath Mar 07 '16 at 12:59
  • 1
    @SandeepanNath Most likely you need these other packages, but you can't use aptitude as Google does since Amazon linux is based on Feroda and uses [yum](http://yum.baseurl.org/) as the package manager. That said, it seems there's a premade Feroda package that contains what you want [here](https://admin.fedoraproject.org/pkgdb/package/rpms/libwebp/). You *should* be able to install this by using the command `yum install libwebp`. According to [this](https://aws.amazon.com/amazon-linux-ami/2014.09-packages/) then `libwebp-0.3.0` should be available. – h2ooooooo Mar 07 '16 at 13:02
  • @SandeepanNath Finally [this forum post](https://www.imagemagick.org/discourse-server/viewtopic.php?t=24125) has info on how to install libwebp as well as its dependencies on an Amazon AMI instance. – h2ooooooo Mar 07 '16 at 13:05
  • I am unable to decide on accepting a best answer yet, because my problem is not solved yet. I am unable to use the cwebp and dwebp commands after yum install libwebp. But I am upvoting to acknowledge your efforts, and so that others can add some more info. However, do consider adding the info in your comments to the answer. – Sandeepan Nath Mar 14 '16 at 12:13
  • @SandeepanNath When you say you're unable to use them is it because you're unable to find the commands or because it didn't install right? – h2ooooooo Mar 14 '16 at 12:16
  • Installation happened rightly. I got this - `Installed: libwebp.x86_64 0:0.3.0-3.5.amzn1 Complete!` However, cwebp, dwebp commands were not found - `bash: dwebp: command not found` – Sandeepan Nath Mar 14 '16 at 12:18
  • @SandeepanNath Try to search for them. `sudo find / | grep webp` (search the root with all subfolders as root, only show results containing "webp"). – h2ooooooo Mar 14 '16 at 12:19
  • I did, I can see the following - `/usr/bin/cwebp /usr/bin/dwebp`. Is that what you are tryng to ensure? – Sandeepan Nath Mar 14 '16 at 12:24
  • @SandeepanNath Absolutely - you should be able to run the commands using the full paths at the very least. I'd try to run it in your terminal first and *then* do it with PHP as PHP doesn't necessarily have the right permissions to launch the files. – h2ooooooo Mar 14 '16 at 12:25
  • Okay, I am able to run the command using full path, but still no success. Getting error - `cannot open input file 'https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw'`. This is the input file, and I can open it on chrome - https://lh3.googleusercontent.com/VRY0O_3L8VH2wxJSTiKPr72PeM5uhPPFEsHzzYdxenddpTI150M0TYpljnZisQaROR0=h256-rw – Sandeepan Nath Mar 14 '16 at 12:27
  • @SandeepanNath Ah yes, you can't use an URL - you need to download it locally first. In PHP you shoud use `file_put_contents` or google "curl save file". In the terminal you can use [`wget `](https://www.gnu.org/software/wget/) – h2ooooooo Mar 14 '16 at 12:28
  • Ohkay. I followed that and successfully converted the webp file into a jpg file after resize (using -scale parameter). Now I would like to know if there is a PHP API for this, which I can call from my PHP script. The API supported by google seems to be for Java, https://developers.google.com/speed/webp/docs/api#simple_decoding_api. Has someone already come up with a PHP API for this and shared it on github? Support for this format would be an added advantage to our feature, so it doesn't matter much if it is not very stable. – Sandeepan Nath Mar 14 '16 at 12:59
  • 1
    @SandeepanNath If you're asking whether someone has made a `webp` php extension that calls the direct API, then I do not know - Google can tell you. If you're asking how to build a wrapper around a binary this depends on how advanced you want to make it. [Here's an example someone did for wkhtmltopdf](https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php). I suggest you just use `shell_exec` to start with, but MAKE SURE to escape all parameters with `escapeshellarg` as you don't want people running commands on your server. – h2ooooooo Mar 14 '16 at 13:04
  • I am able to do the resizing from my PHP script, using shell_exec. So I can accept this answer as the best answer :). Please update the same in the answer. Though it will work only if safe mode is off, I guess. I do not have much idea about how to build a wrapper, but I guess if safe_mode is on, using shell_exec to make a wrapper, like you suggested, won't work. Is that true? – Sandeepan Nath Mar 14 '16 at 14:18
  • 1
    @SandeepanNath Not entirely true. You can enable `safe_mode` if you use [`safe_mode_exec_dir`](http://php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode-exec-dir) and place your binaries in that path. Keep in mind that [according to the documentation](http://php.net/manual/en/features.safe-mode.functions.php) you cannot use `shell_exec` with `safe_mode` on, but you *can* use `exec`, `passthru`, `system` or `popen` (whichever you prefer). The wrapper makes no difference at all though, and is only useful for you, the developer. Imagine you wanted to use `(new WEBP())->convert('foo.webp')` – h2ooooooo Mar 14 '16 at 14:21
  • Hello again. I have, so far, been able to resize the image, using libwebp commands (dwebp), after downloading the image using wget. But I am not yet able to get the dimensions of the original image, before doing the conversion. That was the original question here, and looking at the google API doc, I could not find a way to get the dimensions. Any help would be appreciated. – Sandeepan Nath Mar 21 '16 at 14:02
  • 1
    @SandeepanNath You won't be able to get it _before_ the conversion without PHP 5.5 unless you can find a binary that does it for you. You need to convert it and _then_ get the dimensions. Step 1. Download image. Step 2. Convert image. Step 3. Get dimensions. Step 4. (throw picture out???) – h2ooooooo Mar 21 '16 at 14:32
  • Correct, I checked that converting the image (say into a jpeg), by default, creates an image of the same dimensions. Why don't you update the answer now? I would like to chose this as the best answer? :) – Sandeepan Nath Mar 21 '16 at 14:40
  • @SandeepanNath Done :) – h2ooooooo Mar 21 '16 at 14:45
1

I think it's beacause of unsupported format. Try imagetypes to know what is supported.

$bits = imagetypes();

Check out this post, it can be helpful. After installing you'll be able to do

$image = new Imagick($originalFilepath);
$origImageDimens = $image->getImageGeometry(); 
$origImgWidth = $origImageDimens['width']; 
$origImgHeight = $origImageDimens['height'];   
ksimka
  • 1,394
  • 9
  • 21
  • Is this the way to check that - if (imagetypes() & IMG_PNG) echo "PNG Support is enabled"; – Sandeepan Nath Mar 07 '16 at 12:03
  • Yes, it is. But as you can see there is no const for kinda `IMG_WEBP` in current docs. So I think it's just not supported by design or yet. – ksimka Mar 07 '16 at 12:04
  • Right. I too noticed that. So, does that mean there would no PHP way of finding the dimensions natively? Will using some libraries, like GD library provide some option? Any ideas? – Sandeepan Nath Mar 07 '16 at 12:07
  • 1
    Added a useful link to answer, check out. – ksimka Mar 07 '16 at 12:09
  • I have imagick installed on the server (not PHP Imagemagick though). I checked that even the command line convert is also not supporting the webp format. Since the Imagemagick PHP class is nothing but a wrapper to the imagick utility, I doubt that won't work either when imagick itself is not working for this format. If I run this command - `convert -resize 50x50! `, I am getting this error `convert: no decode delegate for this image format `WEBP' @ error/constitute.c/ReadImage/535.` – Sandeepan Nath Mar 14 '16 at 11:11
  • By the way, the link is no more working. It gives some unreadable output. – Sandeepan Nath Mar 14 '16 at 12:10
  • Ohk, it works fine on chrome. Not on my firefox. By the way, the link is for Ubuntu, but I am working on amazone linux. I guess I missed adding that info in the beginning. – Sandeepan Nath Mar 14 '16 at 12:22
  • Comment here to get missing WEBP support in getimagesize fixed in PHP https://bugs.php.net/bug.php?id=65038. – Artem Russakovskii Jun 10 '16 at 08:18