Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:调整大小/制作缩略图?_Php - Fatal编程技术网

PHP:调整大小/制作缩略图?

PHP:调整大小/制作缩略图?,php,Php,你好,我现在已经完成了我的上传照片系统。现在,我想包括创建不同大小的上传图像缩略图,如48x48和148x50,我如何才能做到这一点 示例/这方面的好教程?当时我使用imagemagick $ convert -resize 48x48 xyz.jpg xyz_48x48.jpg 它还可以作为php模块提供: 但是我没有使用过这个,但是我想它知道的与命令行变量完全相同。您需要使用PHP的GD库或ImageMagick库 首先找出您在开发和生产环境中安装了哪些(如果有的话) 然后根据您想要使用的

你好,我现在已经完成了我的上传照片系统。现在,我想包括创建不同大小的上传图像缩略图,如48x48和148x50,我如何才能做到这一点


示例/这方面的好教程?

当时我使用imagemagick

$ convert -resize 48x48 xyz.jpg xyz_48x48.jpg
它还可以作为php模块提供:


但是我没有使用过这个,但是我想它知道的与命令行变量完全相同。

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

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

然后根据您想要使用的教程开始查找教程。外面有很多


GD通常与PHP5一起预装。

教程中包含了调整大小的课程。将
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);
    }
}
函数makenicepic($srcfile、$tofile、$maxwidth、$maxheight){
//全球美元;
//检查文件是否存在
如果(!file_存在($srcfile)){
返回“”;
}
$dstfile=$tofile;
包括一次(S_ROOT../data/data_setting.php');
////大小
$tow=$maxwidth;
$toh=$maxheight;
$make_max=0;
$maxtow=950;
$maxtoh=700;
$make_max=1;
$im='';
如果($data=getimagesize($srcfile)){
如果($data[2]==1){
$make_max=0;//gif跳过
如果(函数_存在(“imagecreatefromgif”)){
$im=imagecreatefromformgif($srcfile);
}
}elseif($data[2]==2){
如果(函数_存在(“imagecreatefromjpeg”)){
$im=imagecreatefromjpeg($srcfile);
}
}elseif($data[2]==3){
如果(函数_存在(“imagecreatefrompng”)){
$im=imagecreatefrompng($srcfile);
}
}
}
如果(!$im)返回“”;
$srcw=imagesx($im);
$srch=imagesy($im);
$towh=$tow/$toh;
$srcwh=$srcw/$srch;

如果你在谷歌上搜索过它吗?几十个重复:可能是@Notinlist的重复-只有一个“神奇数字”,当它是我自己的特定任务库的一部分时,它就不那么神奇了。
 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;
}