PDA

View Full Version : حرفه ای: الگوریتم برعکس کردن عکس چیه ؟



prans.info
سه شنبه 14 مرداد 1393, 15:09 عصر
سلام دوستان
با چه الگوریتمی یک تثویر که مثلا طرف صورتش به سمت راست هست رو برعکس می کنند و طرف صورتش سمت چپ میشه (تقریبا مثل آینه) .
این به ذهن من رسید :
پیکسل پیکسل از سمت چپ خونده بشه گذاشته بشه به سمت راست ....

2undercover
سه شنبه 14 مرداد 1393, 16:13 عصر
اگر بخواید از توابع GD استفاده بکنید که با این تابع می تونید: http://php.net/manual/en/function.imageflip.php

اما اگه می خواید خودتون یک تابع بنویسید، بله الگوریتمی که گفتید درست هست. این مثالی که خودم نوشتم:


<?php header('Content-Type: image/png;'); $image = imagecreatefrompng('png.png'); imagesavealpha($image, true); imagealphablending($image, true);
$w = imagesx($image); $h = imagesy($image); $half = floor($w / 2);
$flip = imagecreatetruecolor($w, $h); imagealphablending($flip, false); imagefilledrectangle($flip, 0, 0, $w, $h, imagecolorallocatealpha($flip, 255, 255, 255, 127));
for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $index = imagecolorat($image, $x, $y); $color = imagecolorsforindex($image, $index); $color = imagecolorallocatealpha($flip, $color['red'], $color['green'], $color['blue'], $color['alpha']);
if ($x > $half) { imagesetpixel($flip, $half - ($x - $half), $y, $color); imagealphablending($flip, true); } else if ($x <= $half) { imagesetpixel($flip, ($half - $x) + $half, $y, $color); imagealphablending($flip, true); } } }
imagepng($flip); imagedestroy($image); imagedestroy($flip);