1
\$\begingroup\$

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?

\$\endgroup\$
1
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ Thanks, that's awesome. Any ideas on the thumbnail thing? \$\endgroup\$ – watzon May 24 '13 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 May 24 '13 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.