|
03-09-2016, 13:57
|
|
|
|
חבר מתאריך: 25.10.05
הודעות: 1,508
|
|
ציור על תמונה עם צבעים - צבעים מתחלפים בלי שצריך
היי
אני עושה אתר שמציירים עליו ב canvas וזה הופך את זה ל GIF
הכל עובד טוב ויפה חוץ מהקטע עם הצבעים, אני אסביר:
משתמש מצייר על הקנבס וכל תזוזה של העכבר נשמרת בתור אובייקט בתוך מערך ככה:
קוד:
[{x: 1, y:1, radius: 8, color: "blue"}, {x: 2, y:3, radius: 16, color: "red"} ...]
עכשיו זה צבעים מוגדרים מראש , ב php יש לי מערך של צבעים עם RGB לכל אחד ככה:
קוד:
$colors = array(
'brown' => array(
165,
42,
42
),
'cadetblue' => array(
95,
158,
160
),
'chartreuse' => array(
127,
255,
0
),
'chocolate' => array(
210,
105,
30
),
'coral' => array(
255,
127,
80
),
'crimson' => array(
220,
20,
60
)
);
עכשיו כעיקרון אני עובר על כל איבר במערך שנשלח מהקליינט ומצייר אותו עם הצבע שלו ככה:
קוד:
for ($i = 0; $i <= $j; $i++) {
$colorName = $model[$i]->color;
$color = imagecolorallocate($img, $colors[$colorName][0] ,$colors[$colorName][1] , $colors[$colorName][2]);
imagefilledellipse($img, $model[$i]->x, $model[$i]->y, $model[$i]->radius * 2, $model[$i]->radius * 2, $color);
}
תמונות להמחשה, מה שאני מצייר:
[התמונה הבאה מגיעה מקישור שלא מתחיל ב https ולכן לא הוטמעה בדף כדי לשמור על https תקין: http://i.imgur.com/2G6jrxv.png]
מה שאני מקבל:
[התמונה הבאה מגיעה מקישור שלא מתחיל ב https ולכן לא הוטמעה בדף כדי לשמור על https תקין: http://i.imgur.com/estiPwd.gif]
כפי שאתם שמים לב הצבע משתנה באמצע צבע השוקולד לירוק
אני לא יודע למה זה ככה , אם מסתכלים על המידע שנשלח מהלקוח לשרת (תיכף אני אצרף אותו) אפשר לראות שאחרי צבע השוקולד אין את הצבע הירוק הזה אלא יש צבע אחר
הקוד המלא של הציור
קוד:
<?php
set_time_limit(0);
define('JUMPS', 10);
define('DURATIONPERFRAME', 10);
include('classes/gif.php');
$model = json_decode($_POST['data']);
$colors = array(
'brown' => array(
165,
42,
42
),
'cadetblue' => array(
95,
158,
160
),
'chartreuse' => array(
127,
255,
0
),
'chocolate' => array(
210,
105,
30
),
'coral' => array(
255,
127,
80
),
'crimson' => array(
220,
20,
60
)
);
$dir = uniqid() . mt_rand();
$dire = $dir;
mkdir($dir);
$dir .= '/';
$durations = [];
$frames = [];
$length = count($model);
for ($j = JUMPS; $j < $length; $j += JUMPS) {
$img = @imagecreatefromgif('images/base.gif');
for ($i = 0; $i <= $j; $i++) {
$colorName = $model[$i]->color;
$color = imagecolorallocate($img, $colors[$colorName][0] ,$colors[$colorName][1] , $colors[$colorName][2]);
imagefilledellipse($img, $model[$i]->x, $model[$i]->y, $model[$i]->radius * 2, $model[$i]->radius * 2, $color);
}
imagegif($img, $dir . $j . '.gif', 9);
imagedestroy($img);
$durations[] = DURATIONPERFRAME;
$frames[] = $dir . $j . '.gif';
}
$length %= JUMPS;
$length += $j - JUMPS;
$img = @imagecreatefromgif('images/base.gif');
for ($i = 0; $i < $length; $i++) {
$colorName = $model[$i]->color;
$color = imagecolorallocate($img, $colors[$colorName][0] ,$colors[$colorName][1] , $colors[$colorName][2]);
imagefilledellipse($img, $model[$i]->x, $model[$i]->y, $model[$i]->radius * 2, $model[$i]->radius * 2, $color);
}
imagegif($img, $dir . $j . '.gif', 9);
imagedestroy($img);
$durations[] = DURATIONPERFRAME;
$frames[] = $dir . $j . '.gif';
$durations[count($durations) - 1] = 100;
$gc = new GifCreator();
$gc->create($frames, $durations, 0);
$gifBinary = $gc->getGif();
file_put_contents('a.gif', $gifBinary);
$files = glob("$dir*");
foreach ($files as $file)
unlink($file);
rmdir($dire);
המידע שנשלח מהלקוח לשרת
http://pastebin.com/CPzpJq7f
שוב לא לעבור על הכל רק לראות שאחרי הצבע שוקולד אין שבירה של הצבע שמצוייר בתוצאה
_____________________________________
חתימתי העצומה בגודלה הוסרה ע"י השליט הבלתי מעורער שימי, למי שיש בעיה שיפנה אליו.
ד אַל תַּעַן כְּסִיל כְּאִוַּלְתּוֹ פֶּן תִּשְׁוֶה לּוֹ גַם אָתָּה. ה עֲנֵה כְסִיל כְּאִוַּלְתּוֹ פֶּן יִהְיֶה חָכָם בְּעֵינָיו
|
|