$thumbmaxwidth = 75; /* Width of the thumbnail image */ $thumbmaxheight = 75; /* Height of the thumbnail image */ $normalmaxwidth = 200; /* Width of the "mid-sized" image */ $normalmaxheight = 200; /* Height of the "mid-sized" image */ $Rimgbackgroundcolor = 255; /* In RGB, red index to fill the background with (behind image) */ $Gimgbackgroundcolor = 255; /* In RGB, green index to fill the background with (behind image) */ $Bimgbackgroundcolor = 255; /* In RGB, blue index to fill the background with (behind image) */ $err_msg = ""; /* Errors are collected with this variable */ $tempfilename = ""; /* Name of the file used to temporary stage the retrieved file so it can be worked on */ $normalfilename = ""; /* Final name of the smaller size of the original image */ $thumbfilename = ""; /* Final name of the thumbnail size of the original image */ $itemimageurl = ""; /* The full URL (e.g., http://) to the image to have resized */ $curluseragent = "Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0"; /* User agent to identify this image retriever as */ $filePath = ""; /* Grab filepath and modify so we dump the data to the folder "tmpimgs" located one directory above the current directory this script is in */ $filePathRaw = dirname(__FILE__); $filePathSegments = explode("/", $filePathRaw); for ($x = 1; $x < (sizeof($filePathSegments) - 1); $x++) { $filePath = $filePath . "/" . $filePathSegments[$x]; } $filePath = $filePath . "/tmpimgs"; /* Define the names of the files used in the raw image copy process */ $tempfilename = "raw"; /* Does not need to be a filename with extension on Linux */ $normalfilename = "200x200.jpg"; /* Final name of the smaller size of the original image */ $thumbfilename = "75x75.jpg"; /* Final name of the thumbnail size of the original image */ $itemimageurl = "http://www.somesite.com/someimage.jpg"; /* Perform simple validation */ $protocolpos = -1; $protocolpos = strpos(strtolower($itemimageurl), "http"); if ($protocolpos != 0) { $itemimageurl = ""; } if (strlen($itemimageurl) > 255) { $itemimageurl = ""; } /* Begin Processing */ if (strlen($itemimageurl) > 0) { /* Retrieve the image */ $err_status = ""; $tempfilelocation = $filePath . "/" . $tempfilename; $curl = curl_init(); $fp = fopen($tempfilelocation, "w"); curl_setopt($curl, CURLOPT_URL, $itemimageurl); curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_USERAGENT, $curluseragent); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_MAXREDIRS, 2); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects, the number of which is defined in CURLOPT_MAXREDIRS curl_setopt($curl, CURLOPT_FORBID_REUSE, TRUE); curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE); curl_exec($curl); $err_status = curl_error($curl); curl_close($curl); fclose($fp); /* Continue if no error */ if (strlen($err_status) == 0) { /* Load information regarding the local image */ list($width, $height, $type, $attr) = getimagesize($tempfilelocation); /* Place image into the temporary working area */ $imgdata = null; /* NOTE: $type contains a numeric value instead of the IMAGETYPE_ string value. Anotated here for reference PHP 4.3.0+. */ /* $type values: 1 IMAGETYPE_GIF 2 IMAGETYPE_JPEG 3 IMAGETYPE_PNG 4 IMAGETYPE_SWF 5 IMAGETYPE_PSD 6 IMAGETYPE_BMP 7 IMAGETYPE_TIFF (intel) 8 IMAGETYPE_TIFF (motorola) 9 IMAGETYPE_JPC 10 IMAGETYPE_JP2 11 IMAGETYPE_JPX 12 IMAGETYPE_JB2 13 IMAGETYPE_SWC 14 IMAGETYPE_IFF 15 IMAGETYPE_WBMP 16 IMAGETYPE_XBM */ switch($type) { case 1: $imgdata = imagecreatefromgif($tempfilelocation); break; case 2: $imgdata = imagecreatefromjpeg($tempfilelocation); break; case 3: $imgdata = imagecreatefrompng($tempfilelocation); break; case 16: $imgdata = imagecreatefromxbm($tempfilelocation); break; default: break; } $hasimg = 0; if ($imgdata === false) { /* We don't have an image */ $hasimg = -1; } else { $hasimg = 1; } /* Continue if we have either a gif, jpeg, png or xbm */ if ($hasimg == 1) { /* Create thumbnail and resize the original as may be needed */ $originalaspectratio = $width / $height; /* STEP 1: NORMAL RESIZE OF ORIGINAL */ $normalaspectratio = 0; $normalwidth = 0; $normalheight = 0; $xletterbox = 0; $yletterbox = 0; /* Initial resize retaining proportions */ if ($height > $width) { $originalaspectratio = $normalmaxheight / $height; $normalheight = $normalmaxheight; $normalwidth = $width * $originalaspectratio; $xletterbox = round(($normalmaxwidth - $normalwidth) / 2); } elseif ($height == $width) { $originalaspectratio = $normalmaxheight / $height; $normalheight = $normalmaxheight; $normalwidth = $width * $originalaspectratio; $xletterbox = round(($normalmaxwidth - $normalwidth) / 2); } else { $originalaspectratio = $normalmaxwidth / $width; $normalheight = $height * $originalaspectratio; $normalwidth = $normalmaxwidth; $yletterbox = round(($normalmaxheight - $normalheight) / 2); } /* Generate the resized image (as JPG) */ $normalimage = imagecreatetruecolor($normalmaxwidth, $normalmaxheight); $bgcolor = imagecolorallocate($normalimage, $Rimgbackgroundcolor, $Gimgbackgroundcolor, $Bimgbackgroundcolor); imagefill($normalimage, 0, 0, $bgcolor); imagecopyresampled($normalimage, $imgdata, $xletterbox, $yletterbox, 0, 0, $normalwidth, $normalheight, $width, $height); imagejpeg($normalimage, $filePath . "/" . $normalfilename, 90); imagedestroy($normalimage); /* STEP 2: THUMBNAIL RESIZE OF ORIGINAL */ $thumbaspectratio = 0; $thumbwidth = 0; $thumbheight = 0; $xletterbox = 0; $yletterbox = 0; /* Initial resize retaining proportions */ if ($height > $width) { $thumbaspectratio = $thumbmaxheight / $height; $thumbheight = $thumbmaxheight; $thumbwidth = $width * $thumbaspectratio; $xletterbox = round(($thumbmaxwidth - $thumbwidth) / 2); } elseif ($height == $width) { $thumbaspectratio = $thumbmaxheight / $height; $thumbheight = $thumbmaxheight; $thumbwidth = $width * $thumbaspectratio; $xletterbox = round(($thumbmaxwidth - $thumbwidth) / 2); } else { $thumbaspectratio = $thumbmaxwidth / $width; $thumbheight = $height * $thumbaspectratio; $thumbwidth = $thumbmaxwidth; $yletterbox = round(($thumbmaxheight - $thumbheight) / 2); } /* Generate the resized image (as JPG) */ $thumbimage = imagecreatetruecolor($thumbmaxwidth, $thumbmaxheight); $bgcolor = imagecolorallocate($thumbimage, $Rimgbackgroundcolor, $Gimgbackgroundcolor, $Bimgbackgroundcolor); imagefill($thumbimage, 0, 0, $bgcolor); imagecopyresampled($thumbimage, $imgdata, $xletterbox, $yletterbox, 0, 0, $thumbwidth, $thumbheight, $width, $height); imagejpeg($thumbimage, $filePath . "/" . $thumbfilename, 90); imagedestroy($thumbimage); } else { /* We do not have a gif, jpeg, png or xbm file to work with */ $err_msg = "The image needs to be in either a GIF, JPEG, PNG or XBM format."; } } /* END: if (strlen($err_status) == 0) */ else { /* A problem occurred attempting to retrieve the file */ $err_msg = "An error occurred: " . $err_status . "."; } /* Remove temporary file, if present */ if (file_exists($tempfilelocation)) { unlink($tempfilelocation); } } /* END: if (strlen($itemimageurl) > 0) */ else { /* The URL location to the file does not appear to have been included */ $err_msg = "The URL to the image does not appear to have been included or is improperly formatted."; } //print $err_msg;