Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-type: image/jpeg');
- define('IMAGES_PATH', './thumbs');
- define('TMP_PATH', './tmp');
- define('MAX_WIDTH', 150);
- define('MAX_HEIGHT', 150);
- if(empty($_GET['image'])) die('no image specified');
- $outputname = IMAGES_PATH . '/' . md5($_GET['image']);
- if(file_exists($outputname))
- {
- readfile($outputname);
- die();
- }
- $tmpname = TMP_PATH . '/' . uniqid();
- $ch = curl_init(str_replace(" ", "%20", $_GET['image']));
- $fp = fopen($tmpname, 'wb');
- curl_setopt($ch, CURLOPT_FILE, $fp);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_exec($ch);
- fclose($fp);
- if(curl_errno($ch)) die();
- $imagesize = @getimagesize($tmpname);
- if(!$imagesize) die('invalid image specified');
- $source = null;
- $thumb = null;
- $thumb_width = MAX_WIDTH;
- $thumb_height = MAX_HEIGHT;
- $source_width = $source_height = -1;
- $dest_x = $dest_y = 0;
- $source_x = $source_y = 0;
- if($imagesize[0] <= MAX_WIDTH)
- {
- $thumb_width = $source_width = $imagesize[0];
- $thumb_height = $source_height = min($imagesize[1], MAX_HEIGHT);
- $source_y = max($imagesize[1] - $source_height, 0) / 2;
- }
- else if($imagesize[1] <= MAX_HEIGHT)
- {
- $thumb_height = $source_height = $imagesize[1];
- $thumb_width = $source_width = min($imagesize[0], MAX_WIDTH);
- $source_x = max($imagesize[0] - $source_width, 0) / 2;
- }
- if($source_width <= 0)
- {
- if($imagesize[0] >= $imagesize[1])
- {
- $source_height = $imagesize[1];
- $source_width = (MAX_WIDTH/MAX_HEIGHT)*($source_height);
- $source_x = max($imagesize[0] - $source_width, 0) / 2;
- }
- else
- {
- $source_width = $imagesize[0];
- $source_height = (MAX_HEIGHT/MAX_WIDTH)*($source_width);
- $source_y = max($imagesize[1] - $source_height, 0) / 2;
- }
- }
- switch($imagesize['mime'])
- {
- case 'image/jpg':
- case 'image/jpeg':
- $source = imagecreatefromjpeg($tmpname);
- break;
- case 'image/png':
- $source = imagecreatefrompng($tmpname);
- break;
- default: break;
- }
- if(!$source) die('invalid image specified (mime type not supported)');
- $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
- imagecopyresampled($thumb, $source, $dest_x, $dest_y, $source_x, $source_y, $thumb_width, $thumb_height, $source_width, $source_height);
- imagejpeg($thumb, null, 100);
- imagejpeg($thumb, $outputname, 100);
- unlink($tmpname);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement