I use the following 3rd party script to resize and cache images from a URL. It works as expected on URL’s that contain static images. I now find that I need to be able to manipulate dynamically generated images from image URL’s like: http://www.image-online.com/ShowImage.asp?2Id=c&Id=PA1&ImgId=030
Is it actually possible to resize dynamic images on the fly, or at all for that matter?
<?php
////////////////////////////////////////////////////// THIS IS THE FUNCTION_RESIZE.PHP FILE ///////////////////////////////////////////////////////////////////////////////////////
/*
function by Wes Edling .. http://joedesigns.com
feel free to use this in any project, i just ask for a credit in the source code.
a link back to my site would be nice too.
*/
function resize($imagePath,$opts=null){
# start configuration
$cacheFolder = './cache/'; # path to your cache folder, must be writeable by web server
$remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into
$quality = 90; # image quality to use for ImageMagick (0 - 100)
$cache_http_minutes = 20; # cache downloaded http images 20 minutes
$path_to_convert = '/usr/bin/convert';
## you shouldn't need to configure anything else beyond this point
$purl = parse_url($imagePath);
$finfo = pathinfo($imagePath);
$ext = $finfo['extension'];
# check for remote image..
if(isset($purl['scheme']) && $purl['scheme'] == 'http'):
# grab the image, and cache it so we have something to work with..
list($filename) = explode('?',$finfo['basename']);
$local_filepath = $remoteFolder.$filename;
$download_image = true;
if(file_exists($local_filepath)):
if(filemtime($local_filepath) < strtotime('+'.$cache_http_minutes.' minutes')):
$download_image = false;
endif;
endif;
if($download_image == true):
$img = file_get_contents($imagePath);
file_put_contents($local_filepath,$img);
endif;
$imagePath = $local_filepath;
endif;
if(file_exists($imagePath) == false):
$imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath;
if(file_exists($imagePath) == false):
return 'image not found';
endif;
endif;
if(isset($opts['w'])): $w = $opts['w']; endif;
if(isset($opts['h'])): $h = $opts['h']; endif;
$filename = md5_file($imagePath);
if(!empty($w) and !empty($h)):
$newPath = $cacheFolder.$filename.'_w'.$w.'_h'.$h.(isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "").(isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "").'.'.$ext;
elseif(!empty($w)):
$newPath = $cacheFolder.$filename.'_w'.$w.'.'.$ext;
elseif(!empty($h)):
$newPath = $cacheFolder.$filename.'_h'.$h.'.'.$ext;
else:
return false;
endif;
$create = true;
if(file_exists($newPath) == true):
$create = false;
$origFileTime = date("YmdHis",filemtime($imagePath));
$newFileTime = date("YmdHis",filemtime($newPath));
if($newFileTime < $origFileTime):
$create = true;
endif;
endif;
if($create == true):
if(!empty($w) and !empty($h)):
list($width,$height) = getimagesize($imagePath);
$resize = $w;
if($width > $height):
$resize = $w;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = "x".$h;
endif;
else:
$resize = "x".$h;
if(isset($opts['crop']) && $opts['crop'] == true):
$resize = $w;
endif;
endif;
if(isset($opts['scale']) && $opts['scale'] == true):
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -quality ".$quality." ".$newPath;
else:
$cmd = $path_to_convert." ".$imagePath." -resize ".$resize." -size ".$w."x".$h." xc:".(isset($opts['canvas-color'])?$opts['canvas-color']:"transparent")." +swap -gravity center -composite -quality ".$quality." ".$newPath;
endif;
else:
$cmd = $path_to_convert." ".$imagePath." -thumbnail ".(!empty($h) ? 'x':'').$w."".(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\\>" : "")." -quality ".$quality." ".$newPath;
endif;
$c = exec($cmd);
endif;
# return cache file path
return str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath);
}
//////////////////////////////////////////////////////////////// THIS IS THE EXAMPLE.PHP FILE //////////////////////////////////////////////////////////////////////////////////////////////////////////
<?php
# include the function here
include 'function.resize.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>PHP Image Resize - Example</title>
<style>
body {
background: #ffffff;
color: #121212;
font-family: lucida grande;
text-align: center;
}
h1 { font-size: 15px; text-align: center; }
#main { margin: auto; width: 600px; text-align: left; }
.block { margin: 20px; background: #fafafa; padding: 20px; text-align: center; border: 1px solid #cacaca; }
pre { text-align: left; background: #010101; padding: 10px; font-size: 11px; }
pre code { text-align: left; color: #ffffff; }
.block p { color: #343434; font-size: 12px; }
</style>
</head>
<body>
<div id='main'>
<h1>PHP Image Resizer</h1>
<div class='block'>
<?php $settings = array('w'=>150,'h'=>150,'crop'=>true); ?>
<div><img src='<?=resize('http://www.image-online.com/admin.jpg',$settings)?>' border='0' /></div>
<p>Image cropped & resized by width and height from a remote location.</p>
<div><pre><code>src: http://www.image-online.com/admin.jpg<?php echo "\
\
"; print_r($settings)?></code></pre></div>
</div>
</div>
</body>
</html>
?>
Thanks for looking
Colin