编写例程以在水平轴上显示数据(使用PHP gd2,但这不是重点).
轴开始于$min给$max并显示在金刚石$result,这样的图像将围绕300像素宽和高30像素,如下:
例如http://www.testwolke.de/profile.png
另外,在上述的例子中,$min=0,$max=3,$result=0.6.现在,我需要计算一个有意义的比例和标签,在上面的例子中,例如虚线0 .25 .50 .75 1 1.25 ... up to 3,数字标签位于0 1 2 3.
如果$min=-200和$max=600,虚线应该在-200 -150 -100 -50 0 50 100 ... up to 600,数字标签位于-200 -100 0 100 ... up to 600.
用$min=.02和$max=5.80,虚线.02 .5 1 1.5 2 2.5 ... 5.5 5.8和数字.02 1 2 3 4 5 …
我在PHP的这篇文章之后构建了一个Radon转换.
但我的输出不是预期的结果.
输入:

预期成果:

实际结果:

...
我故意使用RGB而不是灰度,因为我想使用这种方法进行图像指纹识别.最后,频道的数量应该不重要,对吧?
现在是代码的时候了.
主要功能:
这是主要功能,做了很多实际工作:
function RadonTransform($filename)
{
$i = imagecreatefromjpeg($filename);
$size = getimagesize($filename);
$center = new Vector2($size[0] / 2, $size[1] / 2);
$d = min(array($size[0], $size[1]));
$u2 = round(M_PI * ($d / 2.0));
$r = imagecreatetruecolor($u2, $d);
for ($z = 0; $z < $u2; $z++)
{
$w2 = M_PI * ($z / $u2);
$w4 = M_PI / 2.0;
$c1 = new Vector2(cos($w2), sin($w2)); $c1->Multiply($d / 2.0)->Add($center);
$c2 = …Run Code Online (Sandbox Code Playgroud) 我从remi repo安装了php 5.5.6,bun phpinfo()显示没有GD库,gd_info()功能不存在.
扩展已启用/etc/php.d/gd.ini,但仍然没有.
我也尝试重新安装php-gd库并重新安装成功,但一切都保持原样.
yum --enablerepo=remi,remi-php55 reinstall php-gd
Loaded plugins: fastestmirror
Setting up Reinstall Process
Loading mirror speeds from cached hostfile
* base: mirrors.supportex.net
* epel: mirror.muntinternet.net
* extras: mirrors.supportex.net
* remi: mirror.1000mbps.com
* remi-php55: mirror.1000mbps.com
* rpmforge: mirror.nl.leaseweb.net
* updates: mirror.muntinternet.net
Resolving Dependencies
--> Running transaction check
---> Package php-gd.x86_64 0:5.5.6-1.el6.remi will be reinstalled
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Reinstalling:
php-gd x86_64 5.5.6-1.el6.remi remi-php55 …Run Code Online (Sandbox Code Playgroud) 我有以下代码在图像上打印文本.我还在文本周围添加了一个调试框.但是,我注意到左边的文本位于PHP给我的框之外imagettfbbox.
这看起来像字体swash的问题.无论如何还有这个吗?我可以弄清楚斜线开始和实际位置之间的距离imagettfbbox吗?
我不认为这是字体的问题,因为我尝试使用一些脚本样式字体,结果是相似的.
<?php
$font = 'scriptin.ttf';
$text = 'Ipsum';
$size = 30;
$image = imagecreatetruecolor(200, 200);
$fontColour = imagecolorallocate($image, hexdec('11'), hexdec('11'), hexdec('11'));
$bgColour = imagecolorallocate($image, hexdec('CC'), hexdec('CC'), hexdec('CC'));
imagefilledrectangle($image, 0, 0, 200, 200, $bgColour);
$dimensions = imagettfbbox($size, 0, $font, $text);
imagefilledrectangle(
$image,
$dimensions[0] + 40,
$dimensions[7] + 50,
$dimensions[2] + 40,
$dimensions[3] + 50,
imagecolorallocate($image, mt_rand(1, 180), mt_rand(1, 180), mt_rand(1, 180))
);
imagettftext(
$image,
$size,
0,
40,
50,
$fontColour,
$font,
$text
);
header('Content-Type: image/png');
imagepng($image); …Run Code Online (Sandbox Code Playgroud) 当我尝试在不正确的png图像上使用php-gd函数时,出现致命PHP错误。这似乎是一种错误,因为相应于功能文档(imagecreatefrompng例如,):
* @return resource an image resource identifier on success, false on errors.
但是,当我尝试使用不正确的图像进行操作时,我有:
Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in /var/www/common/models/Utils.php on line 61
导致此错误的代码很简单:
$handle = imagecreatefrompng($fname);
Run Code Online (Sandbox Code Playgroud)
在此字符串之后没有代码执行。
imagecreatefromstring尝试从相同的字符串创建图像时,具有相同的行为。
我无法“修复”此图片,因为它是用户提供的,因此我需要处理这种情况。
我试图使用try...catch像这样的块:
echo 'start'."\n";
try {
imagecreatefromstring($result);
} catch (\Throwable $e) {
echo 'error'."\n";
return null;
}
echo 'success'."\n";
Run Code Online (Sandbox Code Playgroud)
但是脚本仅输出“开始”,然后消失并显示我上面已发布的错误描述。
Ubuntu 16.04.2,PHP 7.0,php7.0-gd扩展都是最新版本。
所以我无法使用try ... catch块来处理它,我也不知道该如何处理或修复它。有任何想法吗?
UPD:这似乎与环境真的很奇怪,因为当我在Windows(使用PHP 7.0)下运行相同的代码时,会产生正确的“警告”错误。
UPD2:似乎是新的错误https://bugs.php.net/bug.php?id=73986
由于 GD 函数,我目前正在尝试使用图片和 PHP。现在我想修改PNG图片的大小。这是我想调整大小的 PNG 示例:

虚线代表PNG的边框,背景是透明的,我只在大空间中间丢了一颗星星。我想裁剪这颗星星,得到一个简单的星星方块(即使新背景变成空白,也没关系)。
我怎么能有效地做这样的事情?我想做一个循环检查图片的每个像素..试图找到图像的位置,最后根据最小 x / 最大 X 和最小 y / 最大 y 值裁剪一点边距,但如果我开始工作有数百张照片,这会很长。
编辑 :
<?php
$file = "./crop.png";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$image;
switch ($ext){
case 'png':
$image = imagecreatefrompng($file);
break;
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($file);
break;
case 'gif':
$image = imagecreatefromgif($file);
break;
}
$cropped = imagecropauto($image, IMG_CROP_DEFAULT);
if ($cropped !== false) { // in case a new image resource was returned
echo "=> Cropping needed\n";
imagedestroy($image); // we destroy the original …Run Code Online (Sandbox Code Playgroud) 我正在使用 Laravel 框架和图像/干预库来生成带有动态数据的图像。该代码在我的本地计算机上运行得很好,但是当我将其上传到 aws ubuntu 实例上时,它就会显示错误,例如 Internal gd font() not available。我尝试切换到 imagick 和 public_path() 但它仍然显示相同的错误。我的代码如下:
我尝试切换到 imagick 和 public_path() 但它仍然显示相同的错误。我的代码如下:
公共函数generateIdCard(请求$请求){
$user = app('App\Http\Controllers\ApiTokenController')->authenticate($request);
if($user == null)
return response()->json(['message' => 'Token Invalid','status'=>0], 200);
$user = MatchmakerUser::where('id', $user->id)->first();
if($user){
$img=Image::make($this->url2.'2.png');
$filename = 'mmProfilePic_'.$user->id.'.jpg';
$profilepicurl=$this->url.$filename;
$img2=Image::make($profilepicurl)->resize(82,82);
$img->insert($img2,'top-left',22,24);
$name=$user->first_name.$user->last_name;
$img->text($name,115, 50, function($font) {
$font->file('../../../fonts/Poppins-Bold.ttf');
$font->size(18);
$font->color('#fff');
$font->valign('top');
});
$designation=$user->matchmaker_type;
$img->text($designation,115, 73, function($font) {
$font->file('../../../fonts/Poppins-Medium.ttf');
$font->size(10);
$font->color('#fff');
$font->valign('top');
});
$phone=$user->phone_number;
$img->text($phone,31, 129, function($font) {
$font->file('../../../fonts/Poppins-Medium.ttf');
$font->size(10);
$font->color('#4e4e4e');
$font->valign('top');
});
$location=$user->location;
$img->text($location,33, 148, function($font) { …Run Code Online (Sandbox Code Playgroud) Inspired by this code, I am trying to create a simple bar chart able to dynamically create, resize both bars and texts depends on the the $data, $height and $weight:
<?php
$width = 300;
$height = 200;
$font_path = getenv('WINDIR') . DIRECTORY_SEPARATOR . "Fonts" . DIRECTORY_SEPARATOR;
$font = 'arial.ttf';
$data = ['jan'=>30,'fev'=>40,'mar'=>90,'apr'=>77,
'mai'=>33, 'jun'=>44, 'bigggggggg' => 80];
$columns = count($data);
$padding = ($width+$height)/100;
$column_width = $width / $columns;
$image = imagecreate($width, $height);
$gray = imagecolorallocate($image, 0xcc, 0xcc, …Run Code Online (Sandbox Code Playgroud) 我有以下 2 条路线:-
Route::get('resize/avatar', function() {
$image = 'avatar.jpg';
$target_filename_here = 'thumbnail_'.$image;
$ffs = imagecreatefromjpeg($image);
$size = getimagesize($image);
$dst = imagecreatetruecolor(100,100);
$dds = imagecopyresampled($dst,$ffs,0,0,0,0,100,100,$size[0],$size[1]);
$dn = imagepng($dst,$target_filename_here); // adjust format as needed
imagedestroy($ffs);
imagedestroy($dst);
if($dds) {
return Redirect::to('color/');
} else {
return 'Failed to load the Profile Picture';
}
});
Route::get('color/', function() {
if(file_exists('thumbnail_avatar.jpg')) {
$dest = imagecreatefrompng('transcript.png');
$fn = imagecreatefromjpeg('thumbnail_avatar.jpg');
imagecopy($dest, $fn, 550, 830, 0, 0, imagesx($fn), imagesy($fn));
imagejpeg($dest,"test4.jpg",90);
imagedestroy($dest);
imagedestroy($fn);
return HTML::image('test4.jpg');
} else {
return Redirect::to('resize/avatar');
} …Run Code Online (Sandbox Code Playgroud) 我无法在我的服务器上安装 php-gd 扩展。
我有 php 7.2:
PHP 7.2.10-1+0~20181001133426.7+jessie~1.gbpb6e829 (cli) (built: Oct 1 2018 13:50:53) ( NTS )
Run Code Online (Sandbox Code Playgroud)
和 debian 8 Linux:
Debian GNU/Linux 8
Run Code Online (Sandbox Code Playgroud)
安装 GD 扩展的命令:
sudo apt-get install php7.2-gd
Run Code Online (Sandbox Code Playgroud)
和结果:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libc-ares2 libnfsidmap2 libpcre16-3 libpcre32-3 libuuid-perl libv8-3.14.5 php-http-request php-mail-mimedecode php-net-dime php-net-url
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
php7.2-gd
0 upgraded, …Run Code Online (Sandbox Code Playgroud)