Hola!

Okay, here's the situation. I am trying to create a photo gallery where thumbnails are displayed and when they are clicked on, a new window pops up with the detail image. I want the new window to be the same size as the detail image. So I figured I would use the getimagesize function on the image and use the variables from that array for the height and width of the image. The problem I am having is when I try to use getimagesize on the script that is pulling the image from the database it gives me a bunch of errors. I think that it is not recognizing it as an image. Anyway, here's the code. Thanks.

-Jake-

// the script to pull the image from the database. "getimg1.php3"
// USE: <img src="getimg1.php3?id=$id"
<?php
if($id) {
mysql_connect( "localhost", "$db_user", "$db_pass" );
mysql_select_db( "$database" );
$query = "select image1, image1_type from $db_table where id=$id";
$result = mysql_query($query);
$data = mysql_result($result,0, "image1");
$type = mysql_result($result,0, "image1_type");
header("Content-type: $type");
echo $data;
};
?>
// this is working correctly

// the getimagesize script
<?
$image_file = "/getimg1.php3?id=$id";
$image_size = getimagesize($image_file);
print("<img src=\"$image_file\" $image_size[3]>\n");
print("<br>");
print("$image_size[0]<br>");
print("$image_size[1]<br>");
print("$image_size[2]<br>");
?>

// the errors
Warning: Unable to open /getimg1.php3?id=1 in /getimagesize.php3 on line 3

Warning: No such index in string in /getimagesize.php3 on line 4

//image displays here correctly, but with no width or height attributes//

Warning: No such index in string in /getimagesize.php3 on line 6

Warning: No such index in string in /getimagesize.php3 on line 7

Warning: No such index in string in /getimagesize.php3 on line 8

Thanks again!

    $image_file = "/getimg1.php3?id=$id";

    I believe that this is your problem line. When you use getimagesize, you must use an image file, and because php doesnt resolve variables (unlike html) until they are fully parsed, getimagesize is seeing that you are trying to get the image size of the text string: "/getimg1.php3?id=$id"

    So what you need to do is:

    Query the database for the id of the image in the second script as well as the first script. Get the actual location of the image from the database (save the full location as $image_file) and then use the rest of your code as is.

    Hope this helped.
    -David Allison

      OK I tried the script with the path,

      $image_file = "/getimg1.php3?id=1";

      but it still gives me the same errors.
      I think that this is the key error

      Warning: Unable to open /getimg1.php3?id=1 in /getimagesize.php3 on line 3

      for some reason getimagesize is not recognizing "getimg1.php3?id=1" as an image file. The header content type line is there, so I don't know if it possible. Maybe it is possible to get the image height and width when you store the data into MySQL.

      Any ideas?

      Thanks
      -Jake-

        As David mentioned, the line is not fully parsed yet. Break it up into seperate statements. What you want to end up with is the variable $image_file containing the actual value, not a php command.

        • Gary

          I guess this is what I am confused about:

          "Get the actual location of the image from the database (save the full location as $image_file) and then use the rest of your code as is"

          $data in the first script is the image. It is simply a longblob field in the database, that is echoed to the screen w/ the

          header("Content-type: $type");

          tag in front of it. How do I get the "actual location" of the image?

          Thanks for being patient w/ me.
          -Jake-

            It's trying to to a getimagesize() on a php file, and not the image file. make $image_file the path to the image, not a php3 page path. maybe try:
            <?
            $image_file = "img/test.jpg";
            $image_size = getimagesize($image_file);
            print("<img src=\"$image_file\" $image_size[3]>\n");
            print("<br>");
            print("$image_size[0]<br>");
            print("$image_size[1]<br>");
            print("$image_size[2]<br>");
            ?>

            dunno, sounds logical to me.

            matt

              here, just try this: (all in one page)

              <?php
              if($id) {
              mysql_connect( "localhost", "$db_user", "$db_pass" );
              mysql_select_db( "$database" );
              $query = "select image1, image1_type from $db_table where id=$id";
              $result = mysql_query($query);
              $data = mysql_result($result,0, "image1");
              $type = mysql_result($result,0, "image1_type");
              header("Content-type: $type");
              echo $data;
              };
              $image_size = getimagesize($data);
              print("<img src=\"$data\" $image_size[3]>\n");
              print("<br>");
              print("$image_size[0]<br>");
              print("$image_size[1]<br>");
              print("$image_size[2]<br>");
              ?>
              it works <a href='http://mattxstudios.com/images/test.php'>here</a> with this code:
              <?
              $image_size = getimagesize("mattxlogo.gif");
              print("<img src=\"mattxlogo.gif\" $image_size[3]>\n");
              print("<br>");
              print("$image_size[0]<br>");
              print("$image_size[1]<br>");
              print("$image_size[2]<br>");
              ?>

                10 months later

                This doesn't work matt,

                You've repeated the mistake that Jake was making.

                The solution to this is to store image sizes in the database alongside the image when it's uploaded or generated.

                As you strip the image information out of your file upload data, try

                $size=getimagesize($form_data);

                ($form data being the name of your file form input field).

                you can then get height and width by grabbing

                $size[3] and inserting it along with the image.

                This is not only useful for ideas like yours (opening a popup the exact size of the image), it is also good practice always to use height and width tags because your html page layout can load and be displayed in the browser without having downloaded the whole image to determine the size.

                Hope this is helpful,

                Saul.

                  5 months later

                  Further to this thread, what is the best way of getting the IPTC App13 data from the top of a JPEG file? I have the script working locally for a LOCAL image - without a database, but I cannot persuade it that it might want to work remotely...

                  $size = getImageSize($form_data,&$info) is the syntax - but do I need to do anything else to process that $info data (an array?)

                  I have tried the php manual but to no avail...

                  Regards

                  Charlie

                    9 months later
                    9 years later

                    You can use the following stream wrapper to wrap database image/swf raw blob data as a file in PHP.

                    <?php
                    /*
                    	=======================================================================
                    	PHP Blob Data As File Stream v1.0
                    	=======================================================================
                    	This code is released under the MIT License.
                    	(http://www.opensource.org/licenses/MIT)
                    
                    Copyright (C) 2012 Alex Yam <alexyam@live.com>
                    
                    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
                    
                    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
                    
                    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
                    
                    =======================================================================
                    Code Summary
                    =======================================================================
                    A simple class for PHP functions to read and write blob data as a file
                    using a stream wrapper.
                    
                    Particularly useful for running getimagesize() to get the width and
                    height of .SWF Flash files that are stored in the database as blob data.
                    
                    Requests for a simple way to get the width and height of .SWF blob data
                    in PHP have been around for years. So I decided to write and release 
                    this code under the MIT license to save everyone some time, hopefully
                    this will also benefit people in other use cases.
                    
                    Tested on PHP 5.3.10.
                    
                    =======================================================================
                    Usage Example
                    =======================================================================
                    	#Include
                    		include('./blob_data_as_file_stream.php');
                    
                    	#Register the stream wrapper
                    		stream_wrapper_register("BlobDataAsFileStream", "blob_data_as_file_stream");
                    
                    	#Fetch a .SWF file from the Adobe website and store it into a variable.
                    	#Replace this with your own fetch-swf-blob-data-from-database code.
                    		$swf_url = 'http://www.adobe.com/swf/software/flash/about/flashAbout_info_small.swf';
                    		$swf_blob_data = file_get_contents($swf_url);
                    
                    	#Store $swf_blob_data to the data stream
                    		blob_data_as_file_stream::$blob_data_stream = $swf_blob_data;
                    
                    	#Run getimagesize() on the data stream
                    		$swf_info = getimagesize('BlobDataAsFileStream://');
                    		var_dump($swf_info);
                    
                    =======================================================================
                    Usage Output
                    =======================================================================
                    	array(5) {
                    	  [0]=>
                    	  int(159)
                    	  [1]=>
                    	  int(91)
                    	  [2]=>
                    	  int(13)
                    	  [3]=>
                    	  string(23) "width="159" height="91""
                    	  ["mime"]=>
                    	  string(29) "application/x-shockwave-flash"
                    	}
                    
                    =======================================================================
                    Note for PHP version < 5.3.0
                    =======================================================================
                    Replace all 'static::' with 'self::'
                    Reference: http://php.net/manual/en/language.oop5.late-static-bindings.php
                    
                    */
                    
                    class blob_data_as_file_stream {
                    
                    private static $blob_data_position = 0;
                    public static $blob_data_stream = '';
                    
                    public static function stream_open($path,$mode,$options,&$opened_path){
                    	static::$blob_data_position = 0;
                    	return true;
                    }
                    
                    public static function stream_seek($seek_offset,$seek_whence){
                    	$blob_data_length = strlen(static::$blob_data_stream);
                    	switch ($seek_whence) {
                    		case SEEK_SET:
                    			$new_blob_data_position = $seek_offset;
                    			break;
                    		case SEEK_CUR:
                    			$new_blob_data_position = static::$blob_data_position+$seek_offset;
                    			break;
                    		case SEEK_END:
                    			$new_blob_data_position = $blob_data_length+$seek_offset;
                    			break;
                    		default:
                    			return false;
                    	}
                    	if (($new_blob_data_position >= 0) AND ($new_blob_data_position <= $blob_data_length)){
                    		static::$blob_data_position = $new_blob_data_position;
                    		return true;
                    	}else{
                    		return false;
                    	}
                    }
                    
                    public static function stream_tell(){
                    	return static::$blob_data_position;
                    }
                    
                    public static function stream_read($read_buffer_size){
                    	$read_data = substr(static::$blob_data_stream,static::$blob_data_position,$read_buffer_size);
                    	static::$blob_data_position += strlen($read_data);
                    	return $read_data;
                    }
                    
                    public static function stream_write($write_data){
                    	$write_data_length=strlen($write_data);
                    	static::$blob_data_stream =          substr(static::$blob_data_stream,0,static::$blob_data_position).$write_data.substr(static::$blob_data_stream,static::$blob_data_position+=$write_data_length);
                    	return $write_data_length;
                    }
                    
                    public static function stream_eof(){
                    	return static::$blob_data_position >= strlen(static::$blob_data_stream);
                    }
                    
                    }
                    ?>
                    

                      Only ten years since the query was posted !
                      Some kind of record..

                        cretaceous;10996953 wrote:

                        Some kind of record..

                        Not a very spectacular one, either, considering it would be easier and more efficient to simply store the dimensions in the DB in the first place. :p

                          Write a Reply...