使用PHP Imagick将徽标应用于T恤 [英] Applying a logo to a tshirt using PHP Imagick

查看:135
本文介绍了使用PHP Imagick将徽标应用于T恤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手imagemagic又名想象力,我试图按照此主题使用php代码。我试图将这个标志应用到T恤上,但不能通过遵循线索这样做,因为我无法在php中找到大多数方法,比如使用置换贴图开始。我试过的是以下代码:

I'm new to imagemagic aka imagick on php and im trying to follow this thread using php code. I have tried to apply this logo onto a tshirt but couldn't do so by following the threas becuase i cannot find most of the methods in php like using displacement map to start with. What i have tried is the following code:

$image = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/images/VYLZsoD.jpg');    
$logo = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/images/logo.png');
$logo->resizeImage(200, 200, imagick::FILTER_LANCZOS, 1, TRUE);    
header("Content-Type: image/jpg");

$image->compositeimage($logo, Imagick::COMPOSITE_DEFAULT, 400, 260); 
$image->flattenImages();

echo $image;

我想使用线程中显示的步骤创建一个掩码,依此类推使用php代码(而不是通过命令)将徽标应用到T恤上。我甚至使用COMPOSITE_OVERLAY使徽标看起来像是T恤的一部分,但由于透明度,徽标的原始颜色似乎有所减少,点击这里查看我的最终T恤http://i62.tinypic.com/2ahuw6h.png

I would want to use the steps shown in the thread to create a mask and so on in order to apply the logo onto the tshirt using php code (not via command). I have even used "COMPOSITE_OVERLAY" to make the logo look like its part of the tshirt but it seems like the original color of the logo reduces because of the transparency, check here for my final tshirt http://i62.tinypic.com/2ahuw6h.png.


  1. 请告诉我如何使用php中的imagick存档更好的结果(不会减少徽标的颜色)

  2. 我可以在T恤上标记一个区域所以当我拖动徽标时,它不会显示在T恤边框外?

来源t恤http://i58.tinypic.com/154j52b.jpg
来源logo http://i59.tinypic.com/e02051.png

推荐答案

问题的第二部分,如何让折痕显示出来'很好'可能是一个坏主意。尝试它有两个问题:

The second part of your question, how to get the creases to show through 'nicely' is possibly a bad idea. There are two problems with attempting it:

i)T恤中的扭曲是物理位移。虽然你可以看到折痕照明,但是如果没有相同的物理位移,很难让徽标显得逼真。

i) The distortions in the tshirt are physical displacements. Although you can get the crease lighting to show through, it's really hard to get the logo to look realistic without also having the same physical displacement.

ii)颜色真的不是'表现一致。只要让徽标更亮/更暗,与T恤折痕相同,可能会产生不切实际的效果。例如深色T恤+折痕= 50%明亮的背景。明亮的蓝色标志+ 50%更明亮的折痕效果=不切实际的蓝色亮点。

ii) Colors really don't behave consistently. Just having the logo be lighter/darker by the same amount as the tshirt crease may produce unrealistic effects. e.g. dark tshirt + crease = 50% brighter background. Bright blue logo + 50% brighter crease effect = unrealistic looking blue highlight.

我建议使用平坦的T恤作为背景,因为不切实际的效果往往会分散人们的注意力。但你可以这样做:

I would recommend having a flat tshirt as the background as unrealistic effects tend to distract people. But you could do it like this:

function getAverageColorString(\Imagick $imagick) {

    $tshirtCrop = clone $imagick;
    $tshirtCrop->cropimage(100, 100, 90, 50);
    $stats = $tshirtCrop->getImageChannelStatistics();
    $averageRed = $stats[\Imagick::CHANNEL_RED]['mean'];
    $averageRed = intval(255 * $averageRed / \Imagick::getQuantum());
    $averageGreen = $stats[\Imagick::CHANNEL_GREEN]['mean'];
    $averageGreen = intval(255 * $averageGreen / \Imagick::getQuantum());
    $averageBlue = $stats[\Imagick::CHANNEL_BLUE]['mean'];
    $averageBlue = intval(255 * $averageBlue / \Imagick::getQuantum());
    $colorString = "rgb($averageRed, $averageGreen, $averageBlue)";

    return $colorString;
}


$tshirt = new \Imagick(realpath("../images/tshirt/tshirt.jpg"));
$logo = new \Imagick(realpath("../images/tshirt/Logo.png"));
$logo->resizeImage(100, 100, \Imagick::FILTER_LANCZOS, 1, TRUE);

$tshirt->setImageFormat('png');

//First lets find the creases
//Get the average color of the tshirt and make a new image from it.
$colorString = getAverageColorString($tshirt);
$creases = new \Imagick();
$creases->newpseudoimage(
   $tshirt->getImageWidth(),
   $tshirt->getImageHeight(), 
   "XC:".$colorString
);

//Composite difference finds the creases
$creases->compositeimage($tshirt, \Imagick::COMPOSITE_DIFFERENCE, 0, 0);
$creases->setImageFormat('png');
//We need the image negated for the maths to work later. 
$creases->negateimage(true);
//We also want "no crease" to equal 50% gray later
//$creases->brightnessContrastImage(-50, 0); //This isn't in Imagick head yet, but is more sensible than the modulate function.
$creases->modulateImage(50, 100, 100);

//Copy the logo into an image the same size as the shirt image
//to make life easier
$logoCentre = new \Imagick();
$logoCentre->newpseudoimage(
   $tshirt->getImageWidth(),
   $tshirt->getImageHeight(),
   "XC:none"
);
$logoCentre->setImageFormat('png');
$logoCentre->compositeimage($logo, \Imagick::COMPOSITE_SRCOVER, 110, 75);

//Save a copy of the tshirt sized logo
$logoCentreMask = clone $logoCentre;

//Blend the creases with the logo
$logoCentre->compositeimage($creases, \Imagick::COMPOSITE_MODULATE, 0, 0);

//Mask the logo so that only the pixels under the logo come through
$logoCentreMask->compositeimage($logoCentre, \Imagick::COMPOSITE_SRCIN, 0, 0);

//Composite the creased logo onto the shirt
$tshirt->compositeimage($logoCentreMask, \Imagick::COMPOSITE_DEFAULT, 0, 0);

//And Robert is your father's brother
header("Content-Type: image/png");
echo $tshirt->getImageBlob();

生成如下图像:

< img src =https://i.stack.imgur.com/HCenG.pngalt =在此处输入图像说明>

这篇关于使用PHP Imagick将徽标应用于T恤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆