我一直试图让透明度与我的应用程序工作(其中存储在他们面前动态调整大小的图像),我想我终于缩小约太多误导后问题imagealphablending和imagesavealpha.源图像永远不会加载适当的透明度!
// With this line, the output image has no transparency (where it should be
// transparent, colors bleed out randomly or it's completely black, depending
// on the image)
$img = imagecreatefromstring($fileData);
// With this line, it works as expected.
$img = imagecreatefrompng($fileName);
// Blah blah blah, lots of image resize code into $img2 goes here; I finally
// tried just outputting $img instead.
header('Content-Type: image/png');
imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);
imagepng($img);
imagedestroy($img);
Run Code Online (Sandbox Code Playgroud)
从文件加载图像会有一些严重的架构难度; 此代码与从iPhone应用程序查询的JSON API一起使用,在这种情况下(并且更加一致)在POST数据中将图像作为base64编码的字符串上载更容易.我是否绝对 …
我尝试使用php gd创建验证码代码:
$im = imagecreatetruecolor(100, 25);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$imgstring=$_GET['captcha'];
$font = 'RAVIE.TTF';
imagettftext($im, 15, 0, 10, 20, $black, $font, $imgstring);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
Run Code Online (Sandbox Code Playgroud)
GET值与ajax一起发送,如下所示:
var randstring;
function Captcha() {
randstring = Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, 5))).toString(36).slice(1);
$.ajax({
url: 'test_image.php',
data: {'captcha' : randstring},
method: 'GET',
success: function (data){
console.log(data);
},
error: function (){
alert('Error');
return false;
}
});
} …Run Code Online (Sandbox Code Playgroud) 我在通过Alpine映像获得PHP-FPM的GD Jpeg支持时遇到问题。我尝试了我能想到的每种组合以使其正常工作。以下是我的Dockerfile中的片段:
FROM php:7.1-fpm-alpine
RUN apk update \
&& apk upgrade \
&& apk add --no-cache \
freetype \
libpng \
libjpeg-turbo \
freetype-dev \
libpng-dev \
jpeg-dev \
libjpeg \
libjpeg-turbo-dev \
RUN docker-php-ext-configure gd \
--with-freetype-dir=/usr/lib/ \
--with-png-dir=/usr/lib/ \
--with-jpeg-dir=/usr/lib/ \
--with-gd
RUN NUMPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \
&& docker-php-ext-install -j${NUMPROC} gd
Run Code Online (Sandbox Code Playgroud)
当我将其装入容器并运行时php -r 'print_r(gd_info());',得到以下信息:
Array
(
[GD Version] => bundled (2.1.0 compatible)
[FreeType Support] =>
[GIF Read Support] => 1
[GIF Create …Run Code Online (Sandbox Code Playgroud) 这是我想做的一个例子:

结果如下:
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 50)
{
// Load image
$img = imagecreatefromjpeg($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 255, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
// Save the image (overwrite)
imagejpeg($img, $img_src);
imagedestroy($img);
}
Run Code Online (Sandbox Code Playgroud) 我已经实现了这个方法(通过以下php教程)来创建图像的预览:
function createPreview($image_path, $filename) {
header('Content-Type: image/jpeg');
$thumb = imagecreatetruecolor(350, 350);
$source = imagecreatefromjpeg($image_path);
list($width, $height) = getimagesize($image_path);
imagecopyresized($thumb, $source, 0, 0, 0, 0, 350, 350, $width, $height);
imagejpeg($thumb, $filename."_prev.jpg");
}
Run Code Online (Sandbox Code Playgroud)
但我注意到缩放图像损失了很多质量.我怎样才能保持缩放图像的质量(我不能使用想象力,我的服务器不支持它)
在使用GD从WebP版本读取并输出JPEG后,我从图像中丢失了颜色信息,看似是蓝色通道.为什么会发生这种情况,我该如何解决?
$pic = imagecreatefromwebp('https://lh4.ggpht.com/uaLB-tbci94IubJJhDZ4n6vJwGF4i9MpFLXl28LBHjVzLIy-K6fhoSILbM4yJcKqq9I=h900-rw');
imagejpeg($pic, './example.jpg', 80);
imagedestroy($pic);
Run Code Online (Sandbox Code Playgroud)
我正在使用Curl via Proxies用我开发的刮刀下载图像.
不幸的是,它得到的奇怪图像看起来像这些,最后一个是完全空白的:/

有没有人有办法确定图像是否具有大部分灰色或是完全空白/白色,这些确实是损坏的图像?
我已经在这里做了很多关于其他问题的检查,但我对其他解决方案没有太多运气.所以请注意建议这是重复的.
谢谢
在了解imgcolorat之后,我进行了搜索并偶然发现了一些代码.我想出了这个:
<?php
$file = dirname(__FILE__) . "/images/1.jpg";
$img = imagecreatefromjpeg($file);
$imagew = imagesx($img);
$imageh = imagesy($img);
$xy = array();
$last_height = $imageh - 5;
$foo = array();
$x = 0;
$y = 0;
for ($x = 0; $x <= $imagew; $x++)
{
for ($y = $last_height;$y <= $imageh; $y++ )
{
$rgb = @imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b …Run Code Online (Sandbox Code Playgroud) 我已经用mime类型检查了文件.如果它是jpg或gif它与它完美配合
$src = imagecreatefromjpeg($tmpName);
Run Code Online (Sandbox Code Playgroud)
和
$src = imagecreatefromgif($tmpName);
Run Code Online (Sandbox Code Playgroud)
但如果图像是png
$src = imagecreatefrompng($tmpName) ;
src在png情况下变量是空的,但在jpg和gif中它显示了它resource id.
有人会告诉我我需要做什么吗?
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['photo']['tmp_name']);
unset($_FILES["photo"]["type"]);
$_FILES["photo"]["type"] = $mime;
if ((($_FILES["photo"]["type"] == "image/gif") || ($_FILES["photo"]["type"] == "image/jpeg") || ($_FILES["photo"]["type"] == "image/jpg") || ($_FILES["photo"]["type"] == "image/pjpeg") || ($_FILES["photo"]["type"] == "image/x-png") || ($_FILES["photo"]["type"] == "image/png")) && in_array($extension, $allowedExts)) {
if ($_FILES["photo"]["error"] > 0) {
echo "Error uploading file <a href='step-1.php'> Try again. </a>";
$image_check …Run Code Online (Sandbox Code Playgroud) 我试图安装php5-gd但运行此命令后:
yum install php-gd php-tidy
Run Code Online (Sandbox Code Playgroud)
并尝试重新启动apache:
httpd -k restart
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
httpd: Syntax error on line 56 of /etc/httpd/conf/httpd.conf: Syntax error on line 6 of /etc/httpd/conf.modules.d/10-php.conf: Cannot load modules/libphp5.so into server: /etc/httpd/modules/libphp5.so: symbol SSLeay_version, version OPENSSL_1.0.1 not defined in file libcrypto.so.10 with link time reference
Run Code Online (Sandbox Code Playgroud)
你们中的任何人都知道为什么或如何解决这个错误?
基本上,我在我的网站上使用PHP和HTML.我是PHP的新手.所以如果我在我的代码或方法中犯了任何错误,我请求你纠正我.
我编写了用于重新调整用户上传到特定大小(即特定宽度和高度)的图像的代码.我想制作尺寸为940 px*370 px的上传图片.但在这样做的同时,我想照顾以下问题:
因此,为了实现上述功能,我编写了以下代码:
HTML代码(upload_file.html):
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
PHP代码(upload_file.php):
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 5242880)
&& in_array($extension, $allowedExts)) …Run Code Online (Sandbox Code Playgroud) php-gd ×10
php ×8
image ×3
ajax ×1
alpine-linux ×1
apache ×1
docker ×1
file-upload ×1
gd ×1
imagemagick ×1
webp ×1