Tuesday, March 1, 2011

php variable problem

Hello,

I have the following php code, which has been snipped for brevity. I am having trouble with the code after $lastImg is declared, specifically size never seems to get assigned any value, and the hence outputwidth and outputheight remain blank. I have been unable to spot the cause of this.

pic_url is just a string in the database, and definitely points to a valid image. When I run my page without the code to resize, the image is displayed perfectly.

<?php
header("Content-Type: text/html; charset=utf-8");

if (isset($_GET["cmd"]))
    $cmd = $_GET["cmd"];
else
    die("You should have a 'cmd' parameter in your URL");

$pk = $_GET["pk"];
$con = mysql_connect("localhost", "someuser", "notreal");

if(!$con)
{
    die('Connection failed because of' . mysql_error());
}

mysql_query('SET NAMES utf8'); 
mysql_select_db("somedb",$con);

if($cmd == "GetAuctionData")
{
    $sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
    $sql2="SELECT ARTICLE_DESC FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
    $htmlset = mysql_query($sql2);
    $row2 = mysql_fetch_array($htmlset);

    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result))
    {
     $columns = array('FINISHED', 'WATCH', 'PRIVATE_AUCTION', 'REVISED', 'PAYPAL_ACCEPT', 'PRE_TERMINATED', 'PIC_XXL', 'PIC_DIASHOW');
     foreach($columns as $column)
     {
      $$column = $row[$column] ? 'YES' : 'NO';
     }

     $lastImg = $row['PIC_URL']; 
     $maxWidth  = 250;
     $maxHeight = 300;

         $size = getimagesize($_SERVER['DOCUMENT_ROOT'] . $lastImg);
var_dump($size);
echo "SIZE = ".$size."";
if ($size) {
    $imageWidth  = $size[0];
    $imageHeight = $size[1];
    $wRatio = $imageWidth / $maxWidth;
    $hRatio = $imageHeight / $maxHeight;
    $maxRatio = max($wRatio, $hRatio);
else {
die(print_r(error_get_last())); }
else if ($maxRatio > 1) {
        $outputWidth = $imageWidth / $maxRatio;
        $outputHeight = $imageHeight / $maxRatio;
    } else {
        $outputWidth = $imageWidth;
        $outputHeight = $imageHeight;
    }
}

     echo "<h1>".$row['ARTICLE_NAME']."</h1>
      <div id='rightlayer'>
      <img src='".$lastImg."' width='".$outputWidth."' height='".$outputHeight."'>
      </div>";
    }
}

mysql_free_result($result);
From stackoverflow
  • I'd have to say it looks like $row['PIC_URL'] might not point to a valid image, thats the only reason getimagesize would fail. What does your database structure in AUCTIONS look like?

    Joshxtothe4 : pic_url is just a string, however it is definitely pointing to a valid image.
  • 1st. do a

    print_r($row)
    

    just below your

    while ($row = mysql_fetch_array($result))
    

    And see if the array PIC_URL is set there

    2nd you really should do a safe Query.

    $sql="SELECT * FROM AUCTIONS WHERE ARTICLE_NO ='$pk'";
    

    Should be

    $sql = sprintf("SELECT * FROM AUCTIONS WHERE ARTICLE_NO = %d", mysql_real_escape_string($pk));
    

    Same with the other SQL query.

  • You have a few things that conflict with each other:

    $size = getimagesize($lastImg);
    
    echo "<img src='".$lastImg."' ...>"
    

    getimagesize expects a filename while <img> expects a URL.

    You might try something like this:

    $root = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
    
    $size = getimagesize($root . $lastImg);
    

    [EDIT] Sorry. DOCUMENT_ROOT wasn't right.

    Joshxtothe4 : I tried this, and it resulted in a blank page
  • Try:

    $lastImg = $row['PIC_URL'];
    var_dump($row);
    die();
    

    to see if you have anything in $row['PIC_URL'].

    Joshxtothe4 : yes, pic_url is definitly containing a url to a valid image
  • First, to get the right error message, change your code to:

    $size = getimagesize($lastImg);
    echo "SIZE = ".$size."";
    if ($size) {
        $imageWidth  = $size[0];
        $imageHeight = $size[1];
        $wRatio = $imageWidth / $maxWidth;
        $hRatio = $imageHeight / $maxHeight;
        $maxRatio = max($wRatio, $hRatio);
    
        if ($maxRatio > 1) {
            $outputWidth = $imageWidth / $maxRatio;
            $outputHeight = $imageHeight / $maxRatio;
        } else {
            $outputWidth = $imageWidth;
            $outputHeight = $imageHeight;
        }
    } else {
        die(print_r(error_get_last()));
    }
    

    After that, we can look at the error and try to investigate further.

    Joshxtothe4 : this is the error I got: SIZE = Array ( [type] => 2 [message] => getimagesize(http://i10.ebayimg.com/08/i/001/1d/10/8d15_1.JPG?set_id=800005007) [function.getimagesize]: failed to open stream: Operation not permitted [file] => /var/www/intern/ebay/get_auction.php [line] => 48 ) 1
    Sven Lilienthal : URL file-access is not permitted with your server configuration. Do you have access to it?
    Sven Lilienthal : See http://de3.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen for an explanation.
    Joshxtothe4 : I do not really have access to the configuration. Why was it disabled..what were the security concerns? Would it make more sense to download the image locally and save it?
    Sven Lilienthal : I don't know why this option was disabled. Downloading the image should be possible (and the only solution) via curl. fopen() will show the same behaviour as getimagesize()
    Joshxtothe4 : So I would download the image with curl, and then change lastimg to point to where it was downloaded?
    Sven Lilienthal : That seems like the only solution.
    Joshxtothe4 : I have managed to get URL file access enabled. What is the best way to save the image to a file locally without using curl?
    Sven Lilienthal : Just copy it: http://de3.php.net/manual/de/function.copy.php :-)

0 comments:

Post a Comment