Jump to content

getimagesize not always correct...


quasiman

Recommended Posts

Hello all,

I'm trying to write a joomla page that will import LinkShare banners into the banner component.  The problem I'm having is that LinkShare doesn't seem to always have the correct image size listed.  If I use their dimensions, the images come out skewed and bad looking.

So I'm trying to use getimagesize, to get the real dimensions, and it's still not working.

 

Please take a look, and let me know if you have any suggestions.

<?php
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$mainframe =& JFactory::getApplication('site');
$user =& JFactory::getUser();
If (($user->gid) === '25') {
    $token      = "mylinksharetoken"; // required
    $status     = "approved"; //application status name
    $mid        = $_POST['mid'];
    $cat        = $_POST['cat'];
    $size       = $_POST['size']; //optional, use -1 as the default value.
    $campaignID = "-1"; //optional, use -1 as the default value
    $page       = "1"; //optional, use 1 as a default value
    $startdate  = ""; //formatted MMDDYYYY - optional, use an empty set as the default value (//).
    $enddate    = ""; //formatted MMDDYYYY - optional, use an empty set as the default value (//).
    
    if (isset($_POST['submit'])) {
        $feed     = simplexml_load_file("http://lld2.linksynergy.com/services/restLinks/getBannerLinks/{$token}/{$mid}/{$cat}/{$startdate}/{$enddate}/{$size}/{campaignID}/{$page}");
        $children = $feed->children('http://endpoint.linkservice.linkshare.com/');
        $entries  = $children->return;
        
        foreach ($entries as $entry) {
            $sourcecode=GetImageFromUrl($details->imgURL);
            $name=md5(mt_rand());
            $path = '/home/mysite/tmp/imgtmp/';
            $savefile = fopen($path.$name, 'w');
            fwrite($savefile, $sourcecode);
            list($realwidth, $realheight) = getimagesize($path.$name);
            $details = $entry->children('http://endpoint.linkservice.linkshare.com/');
            $image = "<a href=\"{$details->clickURL}\" target=\"_NEW\">";
            $image .= "<img src=\"{$details->imgURL}\" border=\"'0\" height=\"{$realheight}\" width=\"{$realwidth}\" title=\"{$details->name}\" alt=\"{$details->name}\" />";
            $image .= "</a><img border=\"0\" height=\"1\" width=\"1\" src=\"{$details->showURL}\" />";
		echo $image . "<br>\n";
            fclose($savefile);
		@unlink($path.$name);
        }
        
    }
?>
<form name="form1" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
Categories<select name="cat">
<option value="-1">All Categories</option>
<option value="17">Business & Career</option>
<option value="150">B-to-B</option>
<option value="151">Employment</option>
<option value="152">Real Estate</option>
<option value="18">Department Store</option>
<option value="153">Clothing</option>
<option value="154">Gifts</option>
<option value="155">Home</option>
<option value="247">Jewelry</option>
<option value="19">Family</option>
<option value="156">Baby</option>
<option value="157">Education</option>
<option value="158">Entertainment</option>
<option value="159">Pets</option>
<option value="21">Telecommunications</option>
<option value="211">Equipment</option>
<option value="212">Long Distance</option>
<option value="213">Wireless</option>
<option value="1">Hobbies & Collectibles</option>
<option value="101">Art</option>
<option value="102">Auctions</option>
<option value="103">Collectibles</option>
<option value="2">Auto</option>
<option value="104">Accessories</option>
<option value="105">Cars</option>
<option value="106">Rentals</option>
<option value="3">Clothing & Accessories</option>
<option value="207">Children</option>
<option value="107">Accessories</option>
<option value="108">Men</option>
<option value="109">Women</option>
<option value="246">Jewelry</option>
<option value="4">Computer & Electronics</option>
<option value="110">Hardware</option>
<option value="111">Consumer</option>
<option value="112">Software</option>
<option value="5">Entertainment</option>
<option value="113">Books/Magazines</option>
<option value="114">Music</option>
<option value="115">Videos</option>
<option value="6">Financial Services</option>
<option value="116">Banking/Trading</option>
<option value="117">Credit Cards</option>
<option value="118">Loans</option>
<option value="7">Food & Drink</option>
<option value="218">Candy</option>
<option value="119">Cigars</option>
<option value="120">Gourmet</option>
<option value="121">Wine</option>
<option value="8">Games & Toys</option>
<option value="122">Children</option>
<option value="123">Educational</option>
<option value="124">Electronic</option>
<option value="9">Gift & Flowers</option>
<option value="125">Gifts</option>
<option value="126">Flowers</option>
<option value="127">Greeting Cards</option>
<option value="10">Health & Beauty</option>
<option value="229">Prescription</option>
<option value="128">Bath/Body</option>
<option value="129">Cosmetics</option>
<option value="130">Vitamins</option>
<option value="11248">Medical Supplies & Services</option>
<option value="11">Home & Living</option>
<option value="232">Improvement</option>
<option value="131">Bed/Bath</option>
<option value="132">Garden</option>
<option value="133">Kitchen</option>
<option value="12">Mature/Adult</option>
<option value="134">Apparel</option>
<option value="135">Books</option>
<option value="136">Entertainment</option>
<option value="13">Office</option>
<option value="137">Equipment</option>
<option value="138">Home Office</option>
<option value="139">Supplies</option>
<option value="14">Sports & Fitness</option>
<option value="140">Clothing</option>
<option value="141">Collectibles</option>
<option value="142">Equipment</option>
<option value="15">Travel</option>
<option value="245">Vacations</option>
<option value="143">Airline</option>
<option value="144">Car</option>
<option value="145">Hotel</option>
<option value="16">Internet & Online</option>
<option value="149">Programs</option>
<option value="146">Services</option>
<option value="147">Development</option>
<option value="148">Hosting</option>
<option value="16249">Online Dating</option>
<option value="20">Miscellaneous</option>
<option value="210">Other, Other Products/Services</option>
</select>
<?php
    $merchantlist = simplexml_load_file("http://lld2.linksynergy.com/services/restLinks/getMerchByAppStatus/{$token}/{$status}");
    $children2    = $merchantlist->children('http://endpoint.linkservice.linkshare.com/');
    $entries2     = $children2->return;
    echo "Manufacturers: <select name=\"mid\">\n";
    echo "<option selected value=\"-1\">All Manufacturers</option>\n";
    foreach ($entries2 as $entry2) {
        $details2 = $entry2->children('http://endpoint.linkservice.linkshare.com/');
        echo "<option value=\"{$details2->mid}\">{$details2->name}</option>\n";
    }
    echo "</select>";
?>Size: 
<select name="size">
<option selected value="-1">Any Size</option>
<option value="1">468x60 - Full Banner	
<option value="2">392x72 - Mid Banner</option>
<option value="9">160x600 - Wd Skyscrp</option>
<option value="10">120x600 - Skyscraper</option>
<option value="11">180x150 - Rectangle</option>
<option value="12">336x280 - Large Rect</option>
<option value="3">234x60 - Half Banner</option>
<option value="13">300x250 - Med Rect</option>
<option value="14">250x250 - Sq Pop-up</option>
<option value="15">240x400 - Vert Rect</option>
<option value="4">125x125 - Sq Button</option>
<option value="5">120x90 - Button 1</option>
<option value="6">120x60 - Button 2</option>
<option value="7">88x31 - Micro Bar</option>
<option value="8">120x240 - Vert Banner</option>
<option value="0">0x0 - Other</option>
<option value="16">728x90 - Leader-board</option>
<option value="17">720x300 - Pop-Under</option>
<option value="18">550x480 - Pop-Up Large</option>
<option value="19">300x600 - Half Page Ad</option>
<option value="20">305x64 - X-Large</option>
<option value="21">215x64 - Large</option>
<option value="22">167x30 - Medium</option>
<option value="23">112x20 - Small</option>
</select>
<input name="submit" type="submit" value="submit" />
</form>
<?php
} else {
    echo "not logged in";
die();
}
function GetImageFromUrl($link)
{
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result=curl_exec($ch);
curl_close($ch);
	return $result;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/215062-getimagesize-not-always-correct/
Share on other sites

[bump]

Anyone?  I've tried adding in a usleep(250000) and only trying to get the imagesize from the url, but still can't get every image correctly.  When I only try getting one image (that didn't work in the script) it works fine.

IE:

<?php
list($width, $height) = getimagesize("http://images.tigerdirect.com/email/affiliate_new/COMPUSA_Intel_I7_Template_A_468x60.gif");
echo $height . "<br>";
echo $width;
?>

Here's the relevant code as I have it now....

    if (isset($_POST['submit'])) {
        $feed     = simplexml_load_file("http://lld2.linksynergy.com/services/restLinks/getBannerLinks/{$token}/{$mid}/{$cat}/{$startdate}/{$enddate}/{$size}/{$campaignID}/{$page}");
        $children = $feed->children('http://endpoint.linkservice.linkshare.com/');
        $entries  = $children->return;
        foreach ($entries as $entry) {
		usleep(250000);
            list($realwidth, $realheight) = getimagesize($details->imgURL);
            $details = $entry->children('http://endpoint.linkservice.linkshare.com/');
            $image = "<a href=\"{$details->clickURL}\" target=\"_NEW\">";
            $image .= "<img src=\"{$details->imgURL}\" border=\"'0\" height=\"{$realheight}\" width=\"{$realwidth}\" title=\"{$details->name}\" alt=\"{$details->name}\" />";
            $image .= "</a><img border=\"0\" height=\"1\" width=\"1\" src=\"{$details->showURL}\" />";
		echo $image . "<br>\n";
        }
    }

Nevermind, I fixed it.  I assigned a variable to the image url, and then with an if statement checked if it's set.

 

    if (isset($_POST['submit'])) {
        $feed     = simplexml_load_file("http://lld2.linksynergy.com/services/restLinks/getBannerLinks/{$token}/{$mid}/{$cat}/{$startdate}/{$enddate}/{$size}/{$campaignID}/{$page}");
        $children = $feed->children('http://endpoint.linkservice.linkshare.com/');
        $entries  = $children->return;
        foreach ($entries as $entry) {
            $details = $entry->children('http://endpoint.linkservice.linkshare.com/');
		$imageurl = $details->imgURL;
		if(isset($imageurl)) {
			$realsize = getimagesize($imageurl);
		usleep(2500);
            $image = "<a href=\"{$details->clickURL}\" target=\"_NEW\">";
            $image .= "<img src=\"{$imageurl}\" border=\"'0\" width=\"{$realsize['0']}\" height=\"{$realsize['1']}\" title=\"{$details->name}\" alt=\"{$details->name}\" />";
            $image .= "</a><img border=\"0\" height=\"1\" width=\"1\" src=\"{$details->showURL}\" />";
		echo $image . "<br>\n";
		}
        }
    }

Hope this helps someone...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.