我正在尝试完成此功能,除了 1 个小问题外,它似乎工作得很好 - 它似乎将图像定位得太靠左,然后用黑色填充其余空间。
我想要做的是获取此函数以将图像调整为指定的 $thumb_w,如果调整后高度最终大于 $thumb_h,它只会裁剪掉底部。
这是我的函数代码:
function resize_upload ($tmp, $thumb_w, $thumb_h, $img_name, $img_ext, $img_path)
{
if ($img_ext == 'jpg' || $img_ext == 'jpeg' || $img_ext == 'png' || $img_ext == 'gif')
{
if ($img_ext == 'jpg' || $img_ext == 'jpeg')
$source_img = imagecreatefromjpeg($tmp);
else if ($img_ext=='png')
$source_img = imagecreatefrompng($tmp);
else
$source_img = imagecreatefromgif($tmp);
$orig_w = imagesx($source_img);
$orig_h = imagesy($source_img);
$w_ratio = ($thumb_w / $orig_w);
$h_ratio = ($thumb_h / $orig_h);
if ($orig_w > $orig_h )
{
$crop_w = round($orig_w * $h_ratio);
$crop_h = $thumb_h;
$src_x = ceil( ( $orig_w - $thumb_w ) / 2 );
$src_y = 0;
}
elseif ($orig_w < $orig_h )
{
$crop_h = round($orig_h * $w_ratio);
$crop_w = $thumb_w;
$src_x = 0;
$src_y = ceil( ( $orig_h - $thumb_h ) / 2 );
}
else
{
$crop_w = $thumb_w;
$crop_h = $thumb_h;
$src_x = 0;
$src_y = 0;
}
$thumb_img = imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($thumb_img, $source_img, 0 , 0 , $src_x, $src_y, $crop_w, $crop_h, $orig_w, $orig_h);
imagejpeg($thumb_img, $img_path.'/'.$img_name.'.'.$img_ext, 100);
imagedestroy($thumb_img);
imagedestroy($source_img);
}
}
我是这样调用函数的:
resize_upload ($_FILES['image_main']['tmp_name'], 556, 346, $img_name, $img_ext, '../wp-content/themes/my-theme/images/projects');
这是函数执行完操作后图像的最终样子:
看到右边的黑色了吗?它可能是一些我无法弄清楚的数学问题。任何帮助将不胜感激。
最佳答案
使用您的帖子 $thumb_w = 556, $thumb_h = 346 中的相同变量,我们假设发送的图像尺寸完全相同,因此不需要调整大小( 556x346)。
$orig_w = 556;
$orig_h = 346;
$w_ratio = 1;
$h_ratio = 1;
if (556 > 346) //true
{
$crop_w = round(556 * 1); // 556
$crop_h = 346;
$src_x = ceil( ( 556 - 346 ) / 2 ); // ceil( 210 / 2 ) == 105;
$src_y = 0;
}
...
$thumb_img = imagecreatetruecolor(556, 346);
imagecopyresampled($thumb_img, $source_img, 0, 0, 105, 0, 556, 346, 556, 346);
因此,您的代码从源图像的 x = 105 开始,并尝试向右移动 556 像素,但超过该点后仅存在 451 像素。因此,如果我发送了一张 556x346 的图像,它会为图像的水平部分复制 105 到 556 像素的宽度,然后为垂直部分复制 0 到 346 的像素宽度。因此,会显示图像的整个垂直部分,但不会显示整个宽度。
我敢肯定,如果我们对高度大于宽度的图像进行这些相同的计算,我们会遇到同样的问题,因为图像底部有黑色空间。
提示:在编写公式和其他需要大量计算的东西时,请先用最简单的数字进行计算。如果这些都不起作用,那么您显然做错了什么。
关于php - 调整大小后的图像中有黑色方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8977086/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其
我没有理解以下行为(另请参阅inthisSOthread):defdef_testputs'def_test.in'yieldifblock_given?puts'def_test.out'enddef_testdoputs'def_testok'endblock_test=procdo|&block|puts'block_test.in'block.callifblockputs'block_test.out'endblock_test.calldoputs'block_test'endproc_test=procdoputs'proc_test.in'yieldifblock_gi
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我需要尝试一些AES片段。我有一些密文c和一个keyk。密文已使用AES-CBC加密,并在前面加上IV。不存在填充,纯文本的长度是16的倍数。所以我这样做:aes=OpenSSL::Cipher::Cipher.new("AES-128-CCB")aes.decryptaes.key=kaes.iv=c[0..15]aes.update(c[16..63])+aes.final它工作得很好。现在我需要手动执行CBC模式,所以我需要单个block的“普通”AES解密。我正在尝试这个:aes=OpenSSL::Cipher::Cipher.new("AES-128-ECB")aes.dec
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path