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?

asked May 24, 2013 at 16:04
\$\endgroup\$

1 Answer 1

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);
answered May 24, 2013 at 17:25
\$\endgroup\$
2
  • \$\begingroup\$ Thanks, that's awesome. Any ideas on the thumbnail thing? \$\endgroup\$ Commented 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\$ Commented May 24, 2013 at 23:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.