0

我只想能够在此代码中添加图像大小调整功能:

<?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);

有任何想法吗?

4

4 回答 4

1

你可以使用这个功能

    function img_resize($target, $newcopy, $w=1400, $h=1200, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
      $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
      $img = imagecreatefrompng($target);
    } else { 
      $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);

    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    imagejpeg($tci, $newcopy, 80);
}

再简单不过了

于 2013-05-24T17:53:45.540 回答
0

发现这个网站有一个简单的图像大小调整的完整解决方案。

http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

于 2013-05-24T17:52:18.287 回答
0

你试过这个api吗? http://thumbr.it/api 它还可以让您裁剪、添加过滤器等。

OT:根据你的死亡信息,我假设你来自巴西?“Nenhuma imagem definida!” :)

于 2013-06-12T10:05:02.100 回答
0

如果你安装了 Imagick,你可以使用它。

function sendThumbnail($srcPath, $height, $width){
    $im = new Imagick($srcPath);
    if ($width == 0 || $height == 0){
        if ($width == 0){
            $width = $im->getImageHeight() * ($im->getImageHeight()/$height);
        } else {
            $height = $im->getImageWidth() * ($im->getImageWidth()/$width);
        }
    }
    $im->adaptiveResizeImage($width, $height, true);
    header("Content-Type: ".mime_content_type($srcPath));
    echo $im->getImageBlob();
}
于 2013-05-24T17:57:31.580 回答