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

发布于 2024-09-16 15:45:47 字数 96 浏览 4 评论 0原文

你好,我现在已经完成了我的上传个人资料照片系统的制作。现在我想要创建不同尺寸的上传图像的缩略图,例如 48x48 和 148x50,我该怎么做?

示例/好的教程?

Helo i now have finish making my upload profilephoto system. Now i want include creating thumbnails of the uploaded image in different sizes eg 48x48 and 148x50, how can i do this?

Example / good tutorials for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽清梦 2024-09-23 15:45:48

那时我用的是imagemagick。

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

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

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

Back then I used imagemagick.

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

It is also available as a php module: http://php.net/manual/en/book.imagick.php

But I haven't used that one, but I suppose it knows exactly the same as the command line variant.

梦言归人 2024-09-23 15:45:48

这是我的调整大小的课程。将出现的 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);
    }
}

Here my class for resizing. Replace the 150 occurences with a variable.

<?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);
    }
}
梦中楼上月下 2024-09-23 15:45:48
 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;
}

}

 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;
}

}

卖梦商人 2024-09-23 15:45:47

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

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

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

GD 通常预装了 PHP5。

You will need to use PHP's GD library or ImageMagick library.

First find out which, if any, you have installed on your development and production environments.

Then start looking for tutorials depending on which one you want to use. There are many out there.

GD usually comes pre-packed with PHP5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文