SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Resizing URL images (not on server) with PHP

    An XML feed provided to us by our third party has images, I extract these images using xpath.

    However I have found that some of these images are too big and have to be resized.

    Due to the sheer quantity of images on the third parties website I do not want to be downloading the images, or making a copy to our server.

    Therefore, how do I resize images from a third party website using PHP?

    Thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you dont want to use resize at server side (actually its good thing not to do that, very server intensive), why dont you consider adding width / height parameters to html IMG tag

  3. #3
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The images are pulled in a loop via XML XPath.

    Some of the images being pulled in are bigger than others, some are way too big and need resizing.

    If I put a width/height this means this will be applied to all images when its only needed for the biggest images.

    In addition, putting width/height tag on big images will make it look really wierd and out of proportion.

    Thanks.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True

    Quick and dirty way to do is only assign WIDTH, then browser will automatically adjust height without distorting the image.

    If you want to apply the size base on actual image size, try using getimagesize function

  5. #5
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh you mean rescaling the image.

    I am using the getimagesize in this function, it does rescale the image; I just thought there might be a better, more efficient way.

    Code:
    // remove duplicate images
    $imageArray=array_unique($imageArray);
    
    $i=0;
    foreach($imageArray as $field=>$value)
    {
    	$i++;
    	$alt = $doc->Resort ." resort image ".$i;						
    	
    	//print auto_p("BEFORE:" . print_r(getimagesize($value)));
    	
    	$beforeArray = getimagesize($value);
    	if ($beforeArray[0] > 300)
    	{
    		$resizeArray = $this->scale_image($value,$mw=300,$mh=150);
    		//print auto_p("AFTER: " . print_r($resizeArray));
    	} else {
    		$resizeArray = null;
    	} // end if
    	
    	if ( (is_array($resizeArray)) && (sizeof($resizeArray>0)) )
    	{
    		$brochure_data['resort_images'] .= ( '<img class="imgxml" src="'.$value.'" width="'.$resizeArray['w'].'" height="'.$resizeArray['h'].'" alt="'.$alt.'" />' );
    	} else {
    		$brochure_data['resort_images'] .= ( '<img class="imgxml" src="'.$value.'" alt="'.$alt.'" />' );
    	} // end if
    } // next
    
    
    function scale_image($img,$mw='',$mh='')
    { // path max_width max_height
    	if(list($w,$h) = @getimagesize($img))
    	{
    		foreach(array('w','h') as $v)
    		{
    			$m = "m{$v}";
    			if(${$v} > ${$m} && ${$m})
    			{
    				$o = ($v == 'w') ? 'h' : 'w';
    				$r = ${$m} / ${$v}; ${$v} = ${$m}; ${$o} = ceil(${$o} * $r);
    			}
    		}
    
    		$resizeArray['w'] = $w;
    		$resizeArray['h'] = $h;
    
    		return $resizeArray;
    		//return("<img src='{$img}' alt='image' width='{$w}' height='{$h}' />");
    	} // end if
    } // end function
    Just to clarify: I am using this above function, it is working fine and does the job of rescaling; however ideally I'd like it to be thumbnailing -- but I understand that this can cause server load issues.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another option is to just put the img tags in a div with overflow:hidden. Obviously, the image seen by the viewer will be clipped. After the browser loads each image, javascript could be used to try to improve the presentation. For example, checking the dimensions and resizing it if needed(will still look poor).

    Be aware, getimagesize() downloads the image.

  7. #7
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rescaling is working fine and I think is a better solution for the moment.

    Until the 3rd party data source can provide us with thumbnailed images, or some repoistory of images that are scaled down there's little else I can think of to do the resizing/rescaling.

    Thanks.

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are bunch of flash slideshow tools out there, which doest auto resizing and thumnail creation at client end without putting any extra load to server.

    Here is a example of image gallery generated using Flickr XML feed
    http://www.livephysics.com/ptools/fl...ics-photos.php

    It uses Flash SlideShowPro component, you can get it from here
    http://slideshowpro.net/

  9. #9
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •