PHP GD, imagecreatefromstring( ); how to get the image dimensions?

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

Normally I use imagecreatefromjpeg() and then getimagesize(), but with Firefox 3 I need to go round this different. So now im using imagecreatefromstring(), but how do I retreive the image dimensions now?,imagesx() and imagesy() functions seem to work with images made with imagecreatefromstring(), too.,Thanks for contributing an answer to Stack Overflow!, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

for those who still interested :

$image = imagecreatefromstring($img_str);
$w = imagesx($image);
$h = imagesy($image);
88%

getimagesize — Get the size of an image,(PHP 4, PHP 5, PHP 7, PHP 8),This function does not require the GD image library., getimagesize

load more v
72%

The getimagesizefromstring() function is an inbuilt function in PHP which is used to get the size of an image from a string. This function accepts the image data as a string parameter and determines the image size and returns the dimensions with the file type and height/width of the image.,Below programs illustrate the getimagesizefromstring() function in PHP:,PHP | getimagesizefromstring() Function,Reference: http://php.net/manual/en/function.getimagesizefromstring.php

Syntax:

array getimagesizefromstring($imagedata, & $imageinfo)

Output:

array(6) {
   [0] => int(667)[1] => int(184)[2] => int(3)[3] => string(24)
   "width="
   667 " height="
   184 "" ["bits"] => int(8)["mime"] => string(9)
   "image/png"
}

Output:

Width of image: 667
Height of image: 184
Image type: 3
Image attribute: width = "667"
height = "184"
load more v
65%

To create a blank image, use the imagecreatetruecolor function:,When using header("Content-Type: $mimeType"); and image____ to generate only an image to the output, be sure to output nothing else, note even a blank line after ?>. (That can be a difficult 'bug' to track down -- you get no image and no clue as to why.) The general advice is to not include ?> at all here.,other imagecreatefrom* functions.,An image can be created using image* functions, where * is the file format.

To create a blank image, use the imagecreatetruecolor function:

$img = imagecreatetruecolor($width, $height);
load more v
75%

I have found a few things on the web about PHP+GD regarding image manipulation, but none seem to give me what I am looking for.,The code I have to get the image resized is here, I have tried a few things, but am definitely having an issue implementing the secondary process of adding the padding.,This will auto detect the image type (if the imagetype is supported by your install),You will have to modify your resizing code not to be based on the cropped image to start with. Since you want to do the cropping and resizing in one go you need to calculate it independently.

The code I have to get the image resized is here, I have tried a few things, but am definitely having an issue implementing the secondary process of adding the padding.

list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
switch ($imageType) {
   case "image/gif":
      $source = imagecreatefromgif($image);
      break;
   case "image/pjpeg":
   case "image/jpeg":
   case "image/jpg":
      $source = imagecreatefromjpeg($image);
      break;
   case "image/png":
   case "image/x-png":
      $source = imagecreatefrompng($image);
      break;
}
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
imagejpeg($newImage, $image, 80);
chmod($image, 0777);
load more v
40%

imagesx() and imagesy() functions seem to work with images made with imagecreatefromstring(), too.,Do you want to become an expert in JavaScript, PHP and MySQL? ,Understand how to register domain names and put your new site on the web!,Note: Learn JavaScript, PHP and MySQL by building the ultimate social network website from scratch! The complete guide!. Reece Kenney is the instructor of this tutorials.

php
load more v
22%

I found the functions imagesx and imagesy which return the width/height from a resource but I can't find any function that tell me the mime type from a resource. Anyone know of a way to do this?,I'm trying to find the mime type of an image. PHP has the function getimagesize but that only takes a filename, whereas I have an image "resource" instead - i.e. an image created from imagecreatefromstring.,This new function is exactly the same of getimagesize but allows you to retreive the information from a stream.,The only way to go is to catch the image before it gets imagecreatefromstringed, and somehow catch the size information from it.

If you've got access to the binary data of the image (as the use of imagecreatefromstring() suggests), you can detect the file-type "manually":

function image_file_type_from_binary($binary) {
   if (
      !preg_match(
         '/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(?:\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/',
         $binary, $hits
      )
   ) {
      return 'application/octet-stream';
   }
   static $type = array(
      1 => 'image/jpeg',
      2 => 'image/gif',
      3 => 'image/png',
      4 => 'image/x-windows-bmp',
      5 => 'image/tiff',
      6 => 'image/x-ilbm',
   );
   return $type[count($hits) - 1];
}
load more v
60%

 public static
 function copyFromUrl($url, $target) {
    if (!@copy($url, $target)) {
       $http = new JO_Http();
       $http - > useCurl(true);
       if (($host = JO_Validate::validateHost($url)) !== false) {
          $http - > setReferrer('http://'.$host);
       }
       $http - > execute($url);
       if ($http - > error) {
          return false;
       } else {
          $im = @ImageCreateFromString($http - > result);
          if (!$im) {
             return false;
          }
          return @file_put_contents($target, $im);
       }
    } else {
       return true;
    }
 }
load more v