Nato
Nato

Reputation: 162

Get the height of a image no matter what mime type

I do not need to resize this image, all I am trying to accomplish is gathering the actual height of the $img just like a browser would interpret it. I allow a user to upload any image for some reason certain images won't return the dimensions, with just this getImageSize();

$img = $_GET['img'];


unction remoteImage($url){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_RANGE, "0-10240");

$fn = $img;
$raw = curl_exec($ch);
$result = array();

if(file_exists($fn)){
    unlink($fn);
}

if ($raw !== false) {

    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($status == 200 || $status == 206) {

        $result["w"] = 0;
        $result["h"] = 0;

        $fp = fopen($fn, 'x');
        fwrite($fp, $raw);
        fclose($fp);

        $size = getImageSize($fn);

        if ($size===false) {
        //  Cannot get file size information
        } else {
        //  Return width and height
            list($result["w"], $result["h"]) = $size;
        }

    }
}

curl_close ($ch);
 return $result;
}

$outputheight = $result["h"];

Upvotes: 2

Views: 85

Answers (2)

Suchit kumar
Suchit kumar

Reputation: 11869

UPDATED : you can use php function imagesy() to get the height of an image resource. this method needs image resource not path.you have to do something like this:It should work for All type of images

$x="resultproces.gif";
$im = imagecreatefromstring(file_get_contents($x));
echo imagesy($im);

see manuals here:http://php.net/manual/en/function.imagesy.php http://php.net/manual/en/function.imagecreatefromstring.php

Upvotes: 1

user4325726
user4325726

Reputation:

use this... check the images type with their extension then switch case for .jpg, png and gif

$im = imagecreatefromjpeg(path to image); for .jpg

.......imagecreatefrompng(path to image); for .png

echo imagesy($im)

that's how I did at http://www.dpedit.tk

I am not on my pc . so I canot type much.

Upvotes: 0

Related Questions