动态缩放 php 中的图像 jpg/png/gif

发布于 2024-07-30 19:14:06 字数 264 浏览 3 评论 0原文

有没有一种简单的方法可以在php中动态缩放图像?

我想专门使用某种功能,我可以将它插入到我的 heml 中,当然,

<img src=image.php?img=boss.jpg&width=500>

它会将图像缩放到任何高度,将其限制为 500px 宽,

我感谢所有输入,谢谢。

编辑确实需要包含 jpg png 和 gif 文件类型

Is there a simple way of dynamically scaling an image in php?

Id like to specifically use some kind of function where i can insert it into my heml such as

<img src=image.php?img=boss.jpg&width=500>

and of course it would then scale the image to whatever height constrains it to 500px wide

i appreciate all input, thanks.

EDIT does need to include jpg png and gif file types

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

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

发布评论

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

评论(5

寂寞笑我太脆弱 2024-08-06 19:14:06

您可以使用 GD 库并创建一个简单的脚本来根据需要缩放图像。 查看手册

You could use a GD library and create a simple script that would scale the image as you like. Check the manual

自演自醉 2024-08-06 19:14:06

未测试

$file = $_GET('img');
$wid = $_GET('width');

// better ways to do this, but this works in a pinch
$orig = @imagecreatefromjpeg($file);
if ($orig === FALSE) $orig = @imagecreatefromgif($file);
if ($orig === FALSE) $orig = @imagecreatefrompng($file);
if ($orig === FALSE) exit("can't continue; $file is unreadable\n");

// aspect ratio stuff
$sx = imagesx($orig);
$sy = imagesy($orig);
$hyt = round($wid * $sy / $sx);

$img = imagecreatetruecolor($wid, $hyt);
imagecopyresampled($img, $orig, 0, 0, 0, 0, $wid, $hyt, $sx, $sy);
header('Content-type: image/jpeg');
imagejpeg($img);

not tested

$file = $_GET('img');
$wid = $_GET('width');

// better ways to do this, but this works in a pinch
$orig = @imagecreatefromjpeg($file);
if ($orig === FALSE) $orig = @imagecreatefromgif($file);
if ($orig === FALSE) $orig = @imagecreatefrompng($file);
if ($orig === FALSE) exit("can't continue; $file is unreadable\n");

// aspect ratio stuff
$sx = imagesx($orig);
$sy = imagesy($orig);
$hyt = round($wid * $sy / $sx);

$img = imagecreatetruecolor($wid, $hyt);
imagecopyresampled($img, $orig, 0, 0, 0, 0, $wid, $hyt, $sx, $sy);
header('Content-type: image/jpeg');
imagejpeg($img);
抽个烟儿 2024-08-06 19:14:06

将其保存为 image.php,它应该像您希望的那样工作。

<?php

if (array_key_exists('img', $_GET) === true)
{
    if (is_file($_GET['img']) === true)
    {
        $scale = array();

        $scale[] = (array_key_exists('width', $_GET) === true) ? $_GET['width'] : null;
        $scale[] = (array_key_exists('height', $_GET) === true) ? $_GET['height'] : null;

        Image($_GET['img'], implode('*', $scale));
    }
}

function Image($image, $scale = null)
{
    $type = image_type_to_extension(@exif_imagetype($image), false);

    if (function_exists('ImageCreateFrom' . $type) === true)
    {
        $image = call_user_func('ImageCreateFrom' . $type, $image);

        if (is_resource($image) === true)
        {
            $size = array(ImageSX($image), ImageSY($image));

            if (isset($scale) === true)
            {
                $scale = array_filter(explode('*', $scale), 'is_numeric');

                if (count($scale) >= 1)
                {
                    if (empty($scale[0]) === true)
                    {
                        $scale[0] = $scale[1] * $size[0] / $size[1];
                    }

                    else if (empty($scale[1]) === true)
                    {
                        $scale[1] = $scale[0] * $size[1] / $size[0];
                    }
                }

                else
                {
                    $scale = array($size[0], $size[1]);
                }
            }

            else
            {
                $scale = array($size[0], $size[1]);
            }

            $result = ImageCreateTrueColor($scale[0], $scale[1]);

            if (is_resource($result) === true)
            {
                ImageCopyResampled($result, $image, 0, 0, 0, 0, $scale[0], $scale[1], $size[0], $size[1]);

                if (headers_sent() === false)
                {
                    header('Content-Type: image/' . $type);

                    if ($type == 'gif')
                    {
                        return ImageGIF($result, null);
                    }

                    else if ($type == 'png')
                    {
                        return ImagePNG($result, null, 9);
                    }

                    else if ($type == 'jpeg')
                    {
                        return ImageJPEG($result, null, 90);
                    }
                }
            }
        }
    }

    return false;
}

?>

Save this as image.php, it should work like you want it to.

<?php

if (array_key_exists('img', $_GET) === true)
{
    if (is_file($_GET['img']) === true)
    {
        $scale = array();

        $scale[] = (array_key_exists('width', $_GET) === true) ? $_GET['width'] : null;
        $scale[] = (array_key_exists('height', $_GET) === true) ? $_GET['height'] : null;

        Image($_GET['img'], implode('*', $scale));
    }
}

function Image($image, $scale = null)
{
    $type = image_type_to_extension(@exif_imagetype($image), false);

    if (function_exists('ImageCreateFrom' . $type) === true)
    {
        $image = call_user_func('ImageCreateFrom' . $type, $image);

        if (is_resource($image) === true)
        {
            $size = array(ImageSX($image), ImageSY($image));

            if (isset($scale) === true)
            {
                $scale = array_filter(explode('*', $scale), 'is_numeric');

                if (count($scale) >= 1)
                {
                    if (empty($scale[0]) === true)
                    {
                        $scale[0] = $scale[1] * $size[0] / $size[1];
                    }

                    else if (empty($scale[1]) === true)
                    {
                        $scale[1] = $scale[0] * $size[1] / $size[0];
                    }
                }

                else
                {
                    $scale = array($size[0], $size[1]);
                }
            }

            else
            {
                $scale = array($size[0], $size[1]);
            }

            $result = ImageCreateTrueColor($scale[0], $scale[1]);

            if (is_resource($result) === true)
            {
                ImageCopyResampled($result, $image, 0, 0, 0, 0, $scale[0], $scale[1], $size[0], $size[1]);

                if (headers_sent() === false)
                {
                    header('Content-Type: image/' . $type);

                    if ($type == 'gif')
                    {
                        return ImageGIF($result, null);
                    }

                    else if ($type == 'png')
                    {
                        return ImagePNG($result, null, 9);
                    }

                    else if ($type == 'jpeg')
                    {
                        return ImageJPEG($result, null, 90);
                    }
                }
            }
        }
    }

    return false;
}

?>
下壹個目標 2024-08-06 19:14:06
*<form method="post" enctype="multipart/form-data">
<textarea name="base"></textarea>
<input type="submit" name="submit" value="submit" />
</form>*
<?php 

function genRandomPassword()
{
    $length = 8;     
    $characters = '12346789abcdefghjkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXYZ';
    $string = '';
    for ($p = 0; $p < $length; $p++)
    {
        $string .= @$characters[@mt_rand(0, @strlen($characters))];
    }
    return $string;
}

 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
       $name=genRandomPassword(); 

$imageDataEncoded = $_POST['base'];
$imageData = base64_decode($imageDataEncoded);
$source = imagecreatefromstring($imageData);
$angle = 180;
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageName = $name.".jpg";
$imageSave = imagejpeg($rotate,'images/'.$imageName,100);

  /*
  time line image 720*406/480*271/320*181 
  user pic 90*90/110*110/250*250
    */

   list($width,$height)=getimagesize('images/'.$imageName);

  $newwidth=480; $newheight=271;




 //$newheight=($height/$width)*$newwidth;
 $tmp=imagecreatetruecolor($newwidth,$newheight);

 imagecopyresampled($tmp,$source,0,0,0,0,$newwidth,$newheight,
  $width,$height);

  $filename = "images/". $imageName;

  imagejpeg($tmp,$filename,100);

   imagedestroy($source);
   imagedestroy($tmp);
 }
 ?>
*<form method="post" enctype="multipart/form-data">
<textarea name="base"></textarea>
<input type="submit" name="submit" value="submit" />
</form>*
<?php 

function genRandomPassword()
{
    $length = 8;     
    $characters = '12346789abcdefghjkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXYZ';
    $string = '';
    for ($p = 0; $p < $length; $p++)
    {
        $string .= @$characters[@mt_rand(0, @strlen($characters))];
    }
    return $string;
}

 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
       $name=genRandomPassword(); 

$imageDataEncoded = $_POST['base'];
$imageData = base64_decode($imageDataEncoded);
$source = imagecreatefromstring($imageData);
$angle = 180;
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageName = $name.".jpg";
$imageSave = imagejpeg($rotate,'images/'.$imageName,100);

  /*
  time line image 720*406/480*271/320*181 
  user pic 90*90/110*110/250*250
    */

   list($width,$height)=getimagesize('images/'.$imageName);

  $newwidth=480; $newheight=271;




 //$newheight=($height/$width)*$newwidth;
 $tmp=imagecreatetruecolor($newwidth,$newheight);

 imagecopyresampled($tmp,$source,0,0,0,0,$newwidth,$newheight,
  $width,$height);

  $filename = "images/". $imageName;

  imagejpeg($tmp,$filename,100);

   imagedestroy($source);
   imagedestroy($tmp);
 }
 ?>
巷雨优美回忆 2024-08-06 19:14:06

我更喜欢 WideImage 库,因为它真的非常容易使用。

就您而言,您所要做的就是:

$img_path = $_GET['img'];
$new_width = $_GET['width'];

$new_img = wiImage::load($img_path)->resize($new_width);

header('Content-Type: image/jpeg');

echo $new_img->asString('jpg', 80);

并且它支持 jpeg、png、gif、gd,...

I prefer WideImage library, because it's really really easy to use.

In your case, everything you have to do is:

$img_path = $_GET['img'];
$new_width = $_GET['width'];

$new_img = wiImage::load($img_path)->resize($new_width);

header('Content-Type: image/jpeg');

echo $new_img->asString('jpg', 80);

And it supports jpeg, png, gif, gd, ...

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