\$\begingroup\$
\$\endgroup\$
I have the following code:
<?php
include '../../inc/config.php';
$size = isset($_REQUEST['size']) ? $_REQUEST['size'] : 'full';
$image = isset($_REQUEST['image']) ? $_REQUEST['image'] : FALSE;
if(!$image) {
print 'Nenhuma imagem definida!';
exit;
}
$img_hash = $PDO->prepare("SELECT url FROM missionary_photos WHERE hash = :hash");
$img_hash->bindValue(':hash', $image);
$img_hash->execute();
$img_hash = $img_hash->fetchAll(PDO::FETCH_ASSOC);
$img_url = SITE_URL.$img_hash[0]['url'];
// Get the image mime type
function get_mime_type($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
}
$mime_type = get_mime_type($img_url);
header('Content-type: '.$mime_type);
readfile($img_url);
I want to clean up the code a little bit and add the ability to resize the image using a GET or POST. Any ideas?
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
You can use PHP's getimagesize() function to get the MIME type.
<?php
include '../../inc/config.php';
if(!isset($_REQUEST['image'])) die('Nenhuma imagem definida!');
$size = isset($_REQUEST['size']) ? $_REQUEST['size'] : 'full';
$image = $_REQUEST['image'];
$img_hash = $PDO->prepare("SELECT url FROM missionary_photos WHERE hash = :hash");
$img_hash->bindValue(':hash', $image);
$img_hash->execute();
$img_hash = $img_hash->fetchAll(PDO::FETCH_ASSOC);
$img_url = SITE_URL.$img_hash[0]['url'];
$info = getimagesize($img_url);
header('Content-type: '.$info['mime']);
readfile($img_url);
answered May 24, 2013 at 17:25
-
\$\begingroup\$ Thanks, that's awesome. Any ideas on the thumbnail thing? \$\endgroup\$watzon– watzon2013年05月24日 17:34:34 +00:00Commented May 24, 2013 at 17:34
-
1\$\begingroup\$ Glad I could help. Resizing images has been done so many times, and this is really out of the scope of Code Review. I'm sure you could try Stack Overflow or Google for guidance. Here's an example I found on nettuts: net.tutsplus.com/tutorials/php/… Give it a shot and post your revised code for feedback! \$\endgroup\$Johntron– Johntron2013年05月24日 23:12:55 +00:00Commented May 24, 2013 at 23:12
lang-php