将图像大小调整添加到我的代码中

时间:2013-05-24 17:49:46

标签: php image thumbnails image-resizing

我只是希望能够为此代码添加图像大小调整功能:

<?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 个答案:

答案 0 :(得分: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);
}

不能比那更简单

答案 1 :(得分:0)

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

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

答案 2 :(得分: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();
}

答案 3 :(得分:0)

你试过这个api吗? http://thumbr.it/api 它还允许您裁剪,添加过滤器等。

OT:根据你的消息,我认为你来自巴西? 'Nenhuma imagem definida!' :)