首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像压缩失真图像php

图像压缩失真图像php
EN

Stack Overflow用户
提问于 2014-09-19 11:50:42
回答 2查看 980关注 0票数 0

我想复制大小调整,合并一个图像使用图像重放在php。它正在适当地调整大小,但失去了它的质量,或者我可以说它的边缘被破坏了,为什么?下面是我的代码,请查看并给solution.PHP GD-库。

代码语言:javascript
复制
<?php
  $percent = 1.0;
  $filename ='filepath.png';
  $image_source = imagecreatefrompng($filename);
  list($old_width, $old_height) = getimagesize($filename );
  $width=$old_width;
  $height=$old_height;
  $new_width = $old_width * $percent;
  $new_height = $old_height * $percent;
  $transColor=imagecolorallocatealpha($image_source, 0, 0, 0, 0);
  imagecolortransparent($image_source, $transColor);
  $image_p = imagecreatetruecolor($new_width, $new_height);
  imagecopyresampled($image_p, $image_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  header('Content-Type: image/png');
  imagepng($image_p);
  imagedestroy($image_p);
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-19 12:09:56

尝试使用以下代码

代码语言:javascript
复制
<?php
$percent = 1.0;
$filename ='filepath.png';
$image_source = imagecreatefrompng($filename);
list($old_width, $old_height) = getimagesize($filename );
$width=$old_width;
$height=$old_height;
$new_width = $old_width * $percent;
$new_height = $old_height * $percent;
$transColor=imagecolorallocatealpha($image_source, 0, 0, 0, 0);
imagecolortransparent($image_source, $transColor);
$image_p = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
imagecopyresampled($image_p, $image_source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
header('Content-Type: image/png');
imagepng($image_p);
imagedestroy($image_p);
?>
票数 1
EN

Stack Overflow用户

发布于 2016-05-02 07:58:12

下面的代码为我服务,您可以根据您的需求进行定制:

代码语言:javascript
复制
function textOutWithStroke($textSize, $textAngle, $x, $y, $textColor, $textFont, $text, $strokeColor = null, $borderSize = 1, $spacing = 0) {
if ($this->isImageCreated()){
    imagealphablending($this->handle, true);
    $textColor = imagecolorallocatealpha($this->handle, $textColor[0], $textColor[1], $textColor[2], $textColor[3]);
    if($strokeColor === null)
        $strokeColor = $textColor;
    else
        $strokeColor = imagecolorallocatealpha($this->handle, $strokeColor[0], $strokeColor[1], $strokeColor[2], $strokeColor[3]);

    if ($borderSize < 1){
        if ($spacing == 0)
            imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text);
        else{
            for ($i = 0; $i < strlen($text); $i++) {
                $letterBox = imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text[$i]);
                $x += $spacing + ($letterBox[2] - $letterBox[0]);
            }
        }
    }
    else{
        if ($spacing == 0) {
            for ($c1 = $x - $borderSize; $c1 <= $x + $borderSize; $c1++)
                for ($c2 = $y - $borderSize; $c2 <= $y + $borderSize; $c2++)
                    imagettftext($this->handle, $textSize, $textAngle, $c1, $c2, $strokeColor, $textFont, $text);

            imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text);
        }
        else{
            for ($i = 0; $i < strlen($text); $i++) {
                for ($c1 = $x - $borderSize; $c1 <= $x + $borderSize; $c1++)
                    for ($c2 = $y - $borderSize; $c2 <= $y + $borderSize; $c2++)
                        imagettftext($this->handle, $textSize, $textAngle, $c1, $c2, $strokeColor, $textFont, $text[$i]);

                $letterBox = imagettftext($this->handle, $textSize, $textAngle, $x, $y, $textColor, $textFont, $text[$i]);
                $x += $spacing + ($letterBox[2] - $letterBox[0]) + (2 * $borderSize);
            }
        }


    }
    imagealphablending($this->handle, $this->alphaBlending);
    return true;
}
else
    return false;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25933288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档