PHP:调整大小/缩略图?

时间:2010-09-01 08:53:27

标签: php

Helo我现在已经完成了我的上传profilephoto系统。现在我想要包括以不同尺寸创建上传图像的缩略图,例如48x48和148x50,我该怎么做?

示例/好的教程?

5 个答案:

答案 0 :(得分:2)

您需要使用PHP的GD库或ImageMagick库。

首先找出您在开发和生产环境中安装了哪些(如果有)。

然后根据您要使用的教程开始寻找教程。那里有很多。

GD通常预装PHP5。

答案 1 :(得分:0)

当时我使用了imagemagick。

$ convert -resize 48x48 xyz.jpg xyz_48x48.jpg

它也可以作为php模块使用:http://php.net/manual/en/book.imagick.php

但我没有使用过那个,但我想它与命令行变体完全相同。

答案 2 :(得分:0)

答案 3 :(得分:0)

这是我的课程,用于调整大小。用变量替换150次出现。

<?php

/**
 * Takes an image and creates a thumbnail of it.
 */
class ImageThumbnail
{
    private $thumbnail;


    /**
     * Create a new object
     *
     * @param string $source Location of the original image (can be null if set using create())
     * @param string $extension File extension, if it has been obfuscated (e.g. moved to PHP's tmp dir)
     */
    public function __construct($source, $extension)
    {
        if ($source)
        {
            $this->create($source, $extension);
        }
    }


    public function create($source, $extension = null)
    {
        if (!$extension)
        {
            $parts = explode('.', $source); // get the file extension
            $extension = array_pop($parts);
        }
        else
        {
            $extension = ltrim($extension, '.'); // get rid of any prefixing dots
        }


        // Get the images size
        $size = getImageSize($source);

        // Width > height
        if ($size[0] > $size[1])
        {
            $width = 150;
            $height = (int) (150 * $size[1] / $size[0]);
        }
        // Height > width
        else
        {
            $width = (int) (150 * $size[0] / $size[1]);
            $height = 150;
        }

        $readFunc = 'imageCreateFrom'.filenameToImageMime($source);
        if (!$source = $readFunc($source))
        {
            throw new Exception('The source image is unreadable');
        }

        // Create an blank image for the thumbnail
        $thumbnail = imageCreateTrueColor($width, $height);

        // Copy source image onto new image
        imageCopyResized($thumbnail, $source, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);

        $this->thumbnail = $thumbnail;
    }


    public function getThumbnail()
    {
        return $this->thumbnail;
    }


    public function move($target)
    {
        $func = 'image'.filenameToImageMime($target);
        $func($this->thumbnail, $target);
        imageDestroy($this->thumbnail);
    }
}

答案 4 :(得分:0)

 function makenicepic($srcfile,$tofile,$maxwidth,$maxheight) {
    //global $_SGLOBAL;
// check file exist
if (!file_exists($srcfile)) {
    return '';
}
$dstfile = $tofile;

include_once(S_ROOT.'./data/data_setting.php');

// //size
$tow = $maxwidth;
$toh =$maxheight;

$make_max = 0;
$maxtow = 950;
$maxtoh = 700;
$make_max = 1;


$im = '';
if($data = getimagesize($srcfile)) {
    if($data[2] == 1) {
        $make_max = 0;//gif skip
        if(function_exists("imagecreatefromgif")) {
            $im = imagecreatefromgif($srcfile);
        }
    } elseif($data[2] == 2) {
        if(function_exists("imagecreatefromjpeg")) {
            $im = imagecreatefromjpeg($srcfile);
        }
    } elseif($data[2] == 3) {
        if(function_exists("imagecreatefrompng")) {
            $im = imagecreatefrompng($srcfile);
        }
    }
}
if(!$im) return '';

$srcw = imagesx($im);
$srch = imagesy($im);

$towh = $tow/$toh;
$srcwh = $srcw/$srch;
if($towh <= $srcwh){
    $ftow = $tow;
    $ftoh = $ftow*($srch/$srcw);

    $fmaxtow = $maxtow;
    $fmaxtoh = $fmaxtow*($srch/$srcw);
} else {
    $ftoh = $toh;
    $ftow = $ftoh*($srcw/$srch);

    $fmaxtoh = $maxtoh;
    $fmaxtow = $fmaxtoh*($srcw/$srch);
}
if($srcw <= $maxtow && $srch <= $maxtoh) {
    $make_max = 0;
}
if($srcw > $tow || $srch > $toh) {
    if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
        imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);

        if($make_max && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
            imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
        }
    } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
        imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);

        if($make_max && @$maxni = imagecreate($fmaxtow, $fmaxtoh)) {
            imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
        }
    } else {
        return '';
    }
    if(function_exists('imagejpeg')) {
        imagejpeg($ni, $dstfile);
        //big pic
        if($make_max) {
            imagejpeg($maxni, $srcfile);
        }
    } elseif(function_exists('imagepng')) {
        imagepng($ni, $dstfile);

        if($make_max) {
            imagepng($maxni, $srcfile);
        }
    }
    imagedestroy($ni);
    if($make_max) {
        imagedestroy($maxni);
    }
}
imagedestroy($im);

if(!file_exists($dstfile)) {
    return '';
} else {
    return $dstfile;
}

}

相关问题