function draw_bar_graph($width, $height, $data, $max_value, $filename) { /*1. Create the empty graph image*/ $img = imagecreatetruecolor($width, $height); /*2. set a white background with black text and gray graphic*/ $bg_color = imagecolorallocate($img, 255, 255, 255); //white $text_color = imagecolorallocate($img, 255, 255, 255); $bar_color = imagecolorallocate($img, 0, 0, 0); //black $border_color = imagecolorallocate($img, 192, 192, 192); //light gray /*3. fill the background */ imagefilledrectangle($img, 0, 0, $width, $height, $bg_color); /*4. Draw the bars */ $bar_width = $width / (count($data) * 2 + 1); for ($i = 0; $i < count($data); $i++) { imagefilledrectangle($img, $i * $bar_width * 2 + $bar_width, $height, $i * $bar_width * 2 + $bar_width * 2, $height - $height / $max_value * $data[$i][1], $bar_color); imagestringup($img, 5, $i * $bar_width * 2 + $bar_width, $height - 5, $data[$i][0], $text_color); } /*5. Draw a rectangle around the whole thing*/ imagerectangle($img, 0, 0, $width - 1, $height - 1, $border_color); /*6. Draw the range up the left side of graph*/ for ($i = 1; $i <= $max_value; $i++) { imagestring($img, 5, 0, $height - $i * ($height / $max_value), $i, $bar_color); } /*7. output image to a file.*/ imagepng($img, $filename, 5); /*8. destroy image.*/ imagedestroy($img); }
public function getGDImageOfMemoryLine(Watcher $Watcher, $width, $height, $y_scale = 1, $memory_size_scale = 1048576) { $points = $Watcher->getPointsArray(); if (empty($points) || empty($y_scale) || empty($memory_size_scale)) { return false; } $max_y = $height; $max_x = $width; $img = imagecreatetruecolor($max_x, $max_y); $point_color = imagecolorallocate($img, 0, 0, 255); $line_color = imagecolorallocate($img, 0, 255, 0); $string_color = imagecolorallocate($img, 0, 0, 0); $white = imagecolorallocate($img, 255, 255, 255); imagefill($img, 10, 10, $white); $x = 0; $y = 0; $x_step = intval($max_x / 100); foreach ($points as $point) { $value = round($point[$Watcher::point_memory] / $memory_size_scale, 2); $new_x = $x + $x_step; $new_y = $value * $y_scale; imageline($img, $x + 1, $max_y - $y, $new_x, $max_y - $new_y, $line_color); $x = $new_x; $y = $new_y; imagesetpixel($img, $x, $max_y - $y, $point_color); imagestringup($img, 1, $x, $max_y - $y - 5, $point[$Watcher::point_name], $string_color); imagestringup($img, 1, $x, $max_y - $y + 30, $value, $string_color); } return $img; }
public function getCanvas() { $date = zbx_date2str(DATE_TIME_FORMAT_SECONDS); imagestring($this->canvas, 1, $this->width - 120, $this->height - 12, $date, $this->getColor('gray')); imagestringup($this->canvas, 1, $this->width - 10, $this->height - 50, ZABBIX_HOMEPAGE, $this->getColor('gray')); return $this->canvas; }
function draw_bar_graph($width, $height, $data, $max_value, $filename) { // Create the empty graph image $img = imagecreatetruecolor($width, $height); // Set a white background with black text and gray graphics $bg_color = imagecolorallocate($img, 255, 255, 255); // white $text_color = imagecolorallocate($img, 255, 255, 255); // white $bar_color = imagecolorallocate($img, 0, 0, 0); // black $border_color = imagecolorallocate($img, 192, 192, 192); // light gray // Fill the background imagefilledrectangle($img, 0, 0, $width, $height, $bg_color); // Draw the bars $bar_width = $width / (count($data) * 2 + 1); for ($i = 0; $i < count($data); $i++) { imagefilledrectangle($img, $i * $bar_width * 2 + $bar_width, $height, $i * $bar_width * 2 + $bar_width * 2, $height - $height / $max_value * $data[$i][1], $bar_color); imagestringup($img, 5, $i * $bar_width * 2 + $bar_width, $height - 5, $data[$i][0], $text_color); } // Draw a rectangle around the whole thing imagerectangle($img, 0, 0, $width - 1, $height - 1, $border_color); // Draw the range up the left side of the graph for ($i = 1; $i <= $max_value; $i++) { imagestring($img, 5, 0, $height - $i * ($height / $max_value), $i, $bar_color); } // Write the graph image to a file imagepng($img, $filename, 5); imagedestroy($img); }
public function getCanvas() { $grey = get_color($this->canvas, '969696', 50); $date = zbx_date2str(DATE_TIME_FORMAT_SECONDS); imagestring($this->canvas, 1, $this->width - 120, $this->height - 12, $date, $grey); imagestringup($this->canvas, 1, $this->width - 10, $this->height - 50, ZABBIX_HOMEPAGE, $grey); return $this->canvas; }
private function vtext($x, $y, $text, $color) { if ($x < 0) { $x = $this->width + $x; } if ($y < 0) { $y = $this->height + $y; } imagestringup($this->img, 2, $x, $y, $text, $color); }
function FilledArc(&$im, $CenterX, $CenterY, $DiameterX, $DiameterY, $Start, $End, $line_color, $fill_color='none',$key='') { ImageArc($im, $CenterX, $CenterY, $DiameterX, $DiameterY, $Start, $End, $line_color); // To close the arc with 2 lines between the center and the 2 limits of the arc $x = $CenterX + (cos(deg2rad($Start)) * ($DiameterX / 2)); $y = $CenterY + (sin(deg2rad($Start)) * ($DiameterY / 2)); ImageLine($im, $x, $y, $CenterX, $CenterY, $line_color); $x = $CenterX + (cos(deg2rad($End)) * ($DiameterX / 2)); $y = $CenterY + (sin(deg2rad($End)) * ($DiameterY / 2)); ImageLine($im, $x, $y, $CenterX, $CenterY, $line_color); // To fill the arc, the starting point is a point in the middle of the closed space $x = $CenterX + (cos(deg2rad(($Start + $End) / 2)) * ($DiameterX / 4)); $y = $CenterY + (sin(deg2rad(($Start + $End) / 2)) * ($DiameterY / 4)); ImageFillToBorder($im, $x, $y, $line_color, $fill_color); // Ainda faltam vários ajustes para escrita do label na vertical... Anderson Peterle => 08/08/2007 if ($key) imagestringup($im,2,$x,$y,$key,$x); }
function createImage($data, $twidth, $tspace, $height) { $dataName = array(); $dataValue = array(); $i = 0; $j = 0; $k = 0; $num = sizeof($data); foreach ($data as $key => $val) { $dataName[] = $key; $dataValue[] = $val; } $maxnum = max($data); $width = ($twidth + $tspace) * $num + 4; //image's width $im = imagecreate($width + 40, $height + 20); $lineColor = imagecolorallocate($im, 12, 12, 12); $bgColor = imagecolorallocate($im, 255, 233, 233); $tColor = imagecolorallocate($im, 123, 200, 56); imagefill($im, 0, 0, $bgColor); imageline($im, 30, 0, 30, $height - 2, $lineColor); imageline($im, 30, $height - 2, $width + 30 - 2, $height - 2, $lineColor); while ($i < $num) { imagefilledrectangle($im, $i * ($tspace + $twidth) + 40, $height - $dataValue[$i], $i * ($tspace + $twidth) + 40 + $twidth, $height - 3, $tColor); imagestringup($im, 4, $i * ($tspace + $twidth) + $twidth / 2 + 30, $height - 10, $dataName[$i] . "(" . $dataValue[$i] . ")", $lineColor); $i++; } while ($j <= 500 / 10) { imagestringup($im, 4, 2, $height - $j * 10 + 10, $j * 10, $lineColor); $j = $j + 10; } while ($k <= 500 / 10) { if ($k != 0) { imageline($im, 28, $height - $k * 10, 32, $height - $k * 10, $lineColor); } $k = $k + 10; } imagepng($im); }
private function _drawData() { $x = 0; $y0 = $this->_getY(0); foreach ($this->_data as $key => $item) { $y = $this->_getY($item['value']); imagefilledrectangle($this->_img, $x, $y0, $x + $this->_item_width, $y, 0x6564ff); imagerectangle($this->_img, $x, $y0, $x + $this->_item_width, $y, 0x4a49c0); if ($this->_text_dir == 'vertical') { imagestringup($this->_img, 2, $x + 3, $y - 3, number_format($item['value']), 0x4a49c0); imagestringup($this->_img, 2, $x + 3, 299, str_pad($item['label'], 16, ' ', STR_PAD_LEFT), 0x333333); } else { $text_len_px = strlen(number_format($item['value'])) * 7; $text_x = $x + ($this->_item_width - $text_len_px) / 2; imagestring($this->_img, 2, $text_x, $y - 13, number_format($item['value']), 0x4a49c0); $text_len_px = strlen($item['label']) * 7; $text_x = $x + ($this->_item_width - $text_len_px) / 2; imagestring($this->_img, 2, $text_x, 289, $item['label'], 0x333333); } $x += $this->_item_width; } }
function draw_graph($category_array, $width, $height, $max_value, $filename) { $img = imagecreatetruecolor($width, $height); $bg_color = imagecolorallocate($img, 255, 255, 255); // white $text_color = imagecolorallocate($img, 255, 255, 255); // white $bar_color = imagecolorallocate($img, 0, 0, 0); // black $border_color = imagecolorallocate($img, 192, 192, 192); // light gray imagefilledrectangle($img, 0, 0, $width, $height, $bg_color); $bar_width = $width / (count($category_array) * 2 + 1); for ($i = 0; $i < count($category_array); $i++) { imagefilledrectangle($img, $i * $bar_width * 2 + $bar_width, $height, $i * $bar_width * 2 + $bar_width * 2, $height - $height / $max_value * $category_array[$i][1], $bar_color); imagestringup($img, 5, $i * $bar_width * 2 + $bar_width, $height - 5, $category_array[$i][0], $text_color); } imagerectangle($img, 0, 0, $width - 1, $height - 1, $border_color); for ($i = 1; $i <= $max_value; $i++) { imagestring($img, 5, 0, $height - $i * ($height / $max_value), $i, $bar_color); } imagepng($img, $filename, 5); imagedestroy($img); }
imagefilledrectangle($im, $x1 + $shiftX, $yt + $shiftYup, $x1 + $shiftX + 8, $yt + $yu + $shiftYup, imagecolorallocate($im, 235, 235, 235)); } // UNKNOWN $yf = $sizeY * $false[$i] / 100; if ($yf > 0) { imagefilledrectangle($im, $x1 + $shiftX, $yt + $yu + $shiftYup, $x1 + $shiftX + 8, $sizeY + $shiftYup, imagecolorallocate($im, 120, 235, 120)); } // GREEN //SDI($yt.'+'.$yf.'+'.$yu); if ($yt + $yf + $yu > 0) { imagerectangle($im, $x1 + $shiftX, $shiftYup, $x1 + $shiftX + 8, $sizeY + $shiftYup, $black); } } for ($i = 0; $i <= $sizeY; $i += $sizeY / 10) { imagestring($im, 1, $sizeX + 5 + $shiftX, $sizeY - $i - 4 + $shiftYup, $i * ($maxY - $minY) / $sizeY + $minY, $darkred); } imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 39 + 15 * 0, $shiftX + 5, $sizeY + $shiftYup + 35 + 9 + 15 * 0, imagecolorallocate($im, 120, 235, 120)); imagerectangle($im, $shiftX, $sizeY + $shiftYup + 39 + 15 * 0, $shiftX + 5, $sizeY + $shiftYup + 35 + 9 + 15 * 0, $black); imagestring($im, 2, $shiftX + 9, $sizeY + $shiftYup + 15 * 0 + 35, 'FALSE (%)', $black); imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 39 + 15 * 1, $shiftX + 5, $sizeY + $shiftYup + 35 + 9 + 15 * 1, imagecolorallocate($im, 235, 120, 120)); imagerectangle($im, $shiftX, $sizeY + $shiftYup + 39 + 15 * 1, $shiftX + 5, $sizeY + $shiftYup + 15 + 9 + 35 * 1, $black); imagestring($im, 2, $shiftX + 9, $sizeY + $shiftYup + 15 * 1 + 35, 'TRUE (%)', $black); imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 39 + 15 * 2, $shiftX + 5, $sizeY + $shiftYup + 35 + 9 + 15 * 2, imagecolorallocate($im, 220, 220, 220)); imagerectangle($im, $shiftX, $sizeY + $shiftYup + 39 + 15 * 2, $shiftX + 5, $sizeY + $shiftYup + 35 + 9 + 15 * 2, $black); imagestring($im, 2, $shiftX + 9, $sizeY + $shiftYup + 15 * 2 + 35, 'UNKNOWN (%)', $black); imagestringup($im, 0, imagesx($im) - 10, imagesy($im) - 50, 'http://www.zabbix.com', $gray); $end_time = time(NULL); imagestring($im, 0, imagesx($im) - 100, imagesy($im) - 12, 'Generated in ' . ($end_time - $start_time) . ' sec', $gray); ImageOut($im); imagedestroy($im); include_once 'include/page_footer.php';
foreach ($per_host as $index => $percent) { $week_day++; if ($week_day > 6) { $week_day -= 7; } if ($index / 2 == (int) ($index / 2)) { $color = imageColorAllocate($img, 249, 243, 70); $color2 = imageColorAllocate($img, 242, 226, 42); $color3 = imageColorAllocate($img, 226, 210, 34); } else { $color = imageColorAllocate($img, 11, 215, 252); $color2 = imageColorAllocate($img, 7, 203, 239); $color3 = imageColorAllocate($img, 7, 187, 219); } $y1 = round($imageW - $imageW * $percent + 12); imageFilledRectangle($img, $x1, $y1, $x2, $y2, $color); $points = array(0 => $x1, 1 => $y1, 2 => $x1 + 3, 3 => $y1 - 5, 4 => $x1 + $collW + 3, 5 => $y1 - 5, 6 => $x2, 7 => $y1); imageFilledPolygon($img, $points, 4, $color2); $points = array(0 => $x2, 1 => $y1, 2 => $x1 + $collW + 3, 3 => $y1 - 5, 4 => $x1 + $collW + 3, 5 => $y2 - 5, 6 => $x2, 7 => $y2); imageFilledPolygon($img, $points, 4, $color3); // imageTTFtext($img, 7, 90, $x1+8, 50, $colorBlack, BASEDIR.'/assets/fonts/font.ttf', $host_data[$index]); imagestringup($img, 1, $x1 + 3, 52, $host_data[$index], $colorBlack); imageTTFtext($img, 6, 0, $x1 + 3, 66, $colorBlack, BASEDIR . '/assets/fonts/font.ttf', $arr_week[$week_day]); $x1 += $collW; $x2 += $collW; } //Header("Content-type: image/gif"); ImageGIF($img, BASEDIR . $imagecache); ImageDestroy($img); } echo '<img src="' . $imagecache . '?' . date_fixed(SITETIME, "dmY") . '" alt="Неделя" /><br /><br />';
$height = 80 + $max_height; $result = imagecreatetruecolor($width, $height); // bakgrunn $bg_color = imagecolorallocate($result, 40, 0, 40); imagefill($result, 0, 0, $bg_color); // overskrift $text_color = imagecolorallocate($result, 238, 238, 238); imagestring($result, 2, 5, 5, "Statistikk for [gjennomsnitt/dag] {$user['user']}", $text_color); $x = 10; $y = 30 + $max_height; $line_color = imagecolorallocate($result, 150, 150, 150); imageline($result, 8, $y - 3, $width - 8, $y - 3, $line_color); $bar_color = imagecolorallocate($result, 200, 200, 200); $bar2_color = imagecolorallocatealpha($result, 150, 150, 150, 100); $hits_color = imagecolorallocate($result, 150, 150, 150); foreach ($stats as $hour => $hits) { if ($peak == 0) { $bar_h = 0; } else { $bar_h = ceil($hits / $peak * $max_height); } $bar_y1 = $y - $bar_h - 3; $bar_y2 = $bar_y1 + $bar_h; imagefilledrectangle($result, $x + 1, $bar_y1, $x + 12, $bar_y2, $bar_color); imagefilledrectangle($result, $x + 1, $bar_y2, $x + 12, $bar_y2 + 50, $bar2_color); imagestringup($result, 1, $x + 3, $y + 43, str_pad(number_format($hits, 0, ",", " "), 6, " ", STR_PAD_LEFT), $hits_color); imagestring($result, 1, $x + 2, $y, str_pad($hour, 2, "0", STR_PAD_LEFT), $text_color); $x += 13; } header("Content-Type: image/png"); imagepng($result);
} //Draw x points for ($drawXpt = 0; $drawXpt < $numScores; $drawXpt++) { $score = $scoreArray[$drawXpt]; $xPt = $xAxisStart + 2 + $xAxisUnit * $drawXpt; $yPtRough = floor($score / ($yAxisUnit / $yAxisInc)); if ($yAxisMin != "0") { $yOffset = $yAxisMin / 100 * $yAxisInc; $yPt = $yAxisEnd - ($yPtRough - $yOffset); } else { $yPt = $yAxisEnd - $yPtRough; } if ($graphLabels != "off" && $score < 0) { imagestringup($image, 2, $xPt - 7, $yPt - 5, $score, $red); } elseif ($graphLabels != "off") { imagestringup($image, 2, $xPt - 7, $yPt - 5, $score, $black); } imagefilledellipse($image, $xPt, $yPt, 4, 4, $black); $arrayPoints[x][$drawXpt] = $xPt; $arrayPoints[y][$drawXpt] = $yPt; } for ($cntC = 0; $cntC < $numScores; $cntC++) { if ($cntC < $numScores - 1) { $currVal = $cntC; $nextVal = $cntC + 1; } else { $currVal = $cntC; $nextVal = $cntC; } $currPointX = $arrayPoints[x][$currVal]; $currPointY = $arrayPoints[y][$currVal];
protected function generateBars() { $this->finalizeColors(); $barCount = 0; $adjustment = 0; if ($this->bool_user_data_range && $this->data_min >= 0) { $adjustment = $this->data_min * $this->unit_scale; } //reverse array to order data sets in order of priority $this->data_array = array_reverse($this->data_array); //set different offsets based on number of data sets $dataset_offset = 0; switch ($this->data_set_count) { case 2: $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_TWO / 100); break; case 3: $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_THREE / 100); break; case 4: $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_FOUR / 100); break; case 5: $dataset_offset = $this->bar_width * (self::MULTI_OFFSET_FIVE / 100); break; } foreach ($this->data_array as $data_set_num => $data_set) { $lineX2 = null; $xStart = $this->y_axis_x1 + $this->space_width / 2; foreach ($data_set as $key => $item) { $hideBarOutline = false; $x1 = round($xStart + $dataset_offset * $data_set_num); $x2 = round($xStart + $this->bar_width + $dataset_offset * $data_set_num); $y1 = round($this->x_axis_y1 - $item * $this->unit_scale + $adjustment); $y2 = round($this->x_axis_y1); //if we are using a user specified data range, need to limit what's displayed if ($this->bool_user_data_range) { if ($item <= $this->data_range_min) { //don't display, we are out of our allowed display range! $y1 = $y2; $hideBarOutline = true; } elseif ($item >= $this->data_range_max) { //display, but cut off display above range max $y1 = $this->x_axis_y1 - $this->actual_displayed_max_value * $this->unit_scale + $adjustment; } } //draw bar if ($this->bool_bars) { if ($this->bool_gradient) { //draw gradient if desired $this->drawGradientBar($x1, $y1, $x2, $y2, $this->multi_gradient_colors_1[$data_set_num], $this->multi_gradient_colors_2[$data_set_num], $data_set_num); } else { imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $this->multi_bar_colors[$data_set_num]); } //draw bar outline if ($this->bool_bar_outline && !$hideBarOutline) { imagerectangle($this->image, $x1, $y2, $x2, $y1, $this->outline_color); } } // draw line if ($this->bool_line) { $lineX1 = $x1 + $this->bar_width / 2; //MIDPOINT OF BARS, IF SHOWN $lineY1 = $y1; if (isset($lineX2)) { imageline($this->image, $lineX2, $lineY2, $lineX1, $lineY1, $this->line_color[$data_set_num]); $lineX2 = $lineX1; $lineY2 = $lineY1; } else { $lineX2 = $lineX1; $lineY2 = $lineY1; } } // display data points if ($this->bool_data_points) { //dont draw datapoints here or will overlap poorly with line //instead collect coordinates $pointX = $x1 + $this->bar_width / 2; //MIDPOINT OF BARS, IF SHOWN $this->data_point_array[] = array($pointX, $y1); } // display data values if ($this->bool_data_values) { $dataX = $x1 + $this->bar_width / 2 - strlen($item) * self::DATA_VALUE_TEXT_WIDTH / 2; //value to be graphed is equal/over 0 if ($item >= 0) { $dataY = $y1 - self::DATA_VALUE_PADDING - self::DATA_VALUE_TEXT_HEIGHT; } else { //check for item values below user spec'd range if ($this->bool_user_data_range && $item <= $this->data_range_min) { $dataY = $y1 - self::DATA_VALUE_PADDING - self::DATA_VALUE_TEXT_HEIGHT; } else { $dataY = $y1 + self::DATA_VALUE_PADDING; } } //add currency sign, formatting etc if ($this->data_format_array) { $item = $this->applyDataFormats($item); } if ($this->data_currency) { $item = $this->applyDataCurrency($item); } //recenter data position if necessary $dataX -= $this->data_additional_length * self::DATA_VALUE_TEXT_WIDTH / 2; imagestring($this->image, 2, $dataX, $dataY, $item, $this->data_value_color); } //write x axis value if ($this->bool_x_axis_values) { if ($data_set_num == $this->data_set_count - 1) { if ($this->bool_x_axis_values_vert) { if ($this->bool_all_negative) { //we must put values above 0 line $textVertPos = round($this->y_axis_y2 - self::AXIS_VALUE_PADDING); } else { //mix of both pos and neg numbers //write value y axis bottom value (will be under bottom of grid even if x axis is floating due to $textVertPos = round($this->y_axis_y1 + strlen($key) * self::TEXT_WIDTH + self::AXIS_VALUE_PADDING); } $textHorizPos = round($xStart + $this->bar_width / 2 - self::TEXT_HEIGHT / 2); //skip and dispplay every x intervals if ($this->x_axis_value_interval) { if ($this->x_axis_value_interval_counter < $this->x_axis_value_interval) { $this->x_axis_value_interval_counter++; } else { imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color); $this->x_axis_value_interval_counter = 0; } } else { imagestringup($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color); } } else { if ($this->bool_all_negative) { //we must put values above 0 line $textVertPos = round($this->y_axis_y2 - self::TEXT_HEIGHT - self::AXIS_VALUE_PADDING); } else { //mix of both pos and neg numbers //write value y axis bottom value (will be under bottom of grid even if x axis is floating due to $textVertPos = round($this->y_axis_y1 + self::TEXT_HEIGHT * 2 / 3 - self::AXIS_VALUE_PADDING); } //horizontal data keys $textHorizPos = round($xStart + $this->bar_width / 2 - strlen($key) * self::TEXT_WIDTH / 2); //skip and dispplay every x intervals if ($this->x_axis_value_interval) { if ($this->x_axis_value_interval_counter < $this->x_axis_value_interval) { $this->x_axis_value_interval_counter++; } else { imagestring($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color); $this->x_axis_value_interval_counter = 0; } } else { imagestring($this->image, 2, $textHorizPos, $textVertPos, $key, $this->x_axis_text_color); } } } } $xStart += $this->bar_width + $this->space_width; } } }
function AStextInternal($p, $st, $pos, $angle) { /*if (func_num_args()>1) { $p = $this->pt2arr($arg); $st = func_get_arg(1); if (func_num_args()>2) { $pos = func_get_arg(2); } if (func_num_args()>3) { $angle = func_get_arg(3); } } else { $p = $this->pt2arr($arg[0]); $st = $arg[1]; if (isset($arg[2])) { $pos = $arg[2]; } if (isset($arg[3])) { $angle = $arg[3]; } }*/ /*else { if (preg_match('/\s*\[(.*?)\]\s*,\s*[\'"](.*?)[\'"]\s*,([^,]*)/',$arg,$m)) { $p = $this->pt2arr($m[1]); $st = $m[2]; $pos = trim(str_replace(array('"',"'"),'',$m[3])); } else { $arg = explode(',',$arg); $p = $this->pt2arr($arg[0].','.$arg[1]); $st = str_replace(array('"',"'"),'',$arg[2]); } }*/ if ($this->usettf) { $bb = imagettfbbox($this->fontsize, $angle, $this->fontfile, $st); $bbw = $bb[4] - $bb[0]; $bbh = -1 * ($bb[5] - $bb[1]); $p[0] = $p[0] - 0.5 * $bbw; $p[1] = $p[1] + 0.5 * $bbh; if ($pos == 'above' || $pos == 'aboveright' || $pos == 'aboveleft') { $p[1] = $p[1] - 0.5 * abs($bbh) - $this->fontsize / 2; } if ($pos == 'below' || $pos == 'belowright' || $pos == 'belowleft') { $p[1] = $p[1] + 0.5 * abs($bbh) + $this->fontsize / 2; } if ($pos == 'left' || $pos == 'aboveleft' || $pos == 'belowleft') { $p[0] = $p[0] - 0.5 * abs($bbw) - $this->fontsize / 2; } if ($pos == 'right' || $pos == 'aboveright' || $pos == 'belowright') { $p[0] = $p[0] + 0.5 * abs($bbw) + $this->fontsize / 2; } if ($this->fontfill != '') { $color = $this->fontfill; } else { $color = $this->stroke; } imagettftext($this->img, $this->fontsize, $angle, $p[0], $p[1], $this->{$color}, $this->fontfile, $st); } else { if ($this->fontsize < 9) { $fs = 1; } else { if ($this->fontsize < 13) { $fs = 2; } else { $fs = 4; } } if ($angle == 90 || $angle == 270) { $bb = array(imagefontheight($fs), imagefontwidth($fs) * strlen($st)); } else { $bb = array(imagefontwidth($fs) * strlen($st), imagefontheight($fs)); } $p[0] = $p[0] - 0.5 * $bb[0]; if ($angle == 90 || $angle == 270) { $p[1] = $p[1] + 0.5 * $bb[1]; } else { $p[1] = $p[1] - 0.5 * $bb[1]; } if ($pos == 'above' || $pos == 'aboveright' || $pos == 'aboveleft') { $p[1] = $p[1] - 0.5 * $bb[1] - $fs * 2; } if ($pos == 'below' || $pos == 'belowright' || $pos == 'belowleft') { $p[1] = $p[1] + 0.5 * $bb[1] + $fs * 2; } if ($pos == 'left' || $pos == 'aboveleft' || $pos == 'belowleft') { $p[0] = $p[0] - 0.5 * $bb[0] - $fs * 2; } if ($pos == 'right' || $pos == 'aboveright' || $pos == 'belowright') { $p[0] = $p[0] + 0.5 * $bb[0] + $fs * 2; } if ($this->fontfill != '') { $color = $this->fontfill; } else { $color = $this->stroke; } if ($angle == 90 || $angle == 270) { imagestringup($this->img, $fs, $p[0], $p[1], $st, $this->{$color}); } else { imagestring($this->img, $fs, $p[0], $p[1], $st, $this->{$color}); } } }
#legend centeredtext($im, ' ', 1, 570, 183, 638, 265, $black, $white, $black); centeredtext($im, ' 1 day ', 1, 571, 184, 637, 200, $black, $white, $white); imagefilledarc($im, 576, 192, 9, 9, 0, 360, $red, IMG_ARC_PIE); centeredtext($im, ' 2 days ', 1, 571, 200, 637, 216, $black, $white, $white); imagefilledarc($im, 576, 208, 7, 7, 0, 360, $blue, IMG_ARC_PIE); centeredtext($im, ' 3 days ', 1, 571, 216, 637, 232, $black, $white, $white); imagefilledarc($im, 576, 224, 5, 5, 0, 360, $green, IMG_ARC_PIE); centeredtext($im, ' 4 days ', 1, 571, 232, 637, 248, $black, $white, $white); imagefilledarc($im, 576, 240, 3, 3, 0, 360, $black, IMG_ARC_PIE); centeredtext($im, ' 5+ days ', 1, 571, 248, 637, 264, $black, $white, $white); imagesetpixel($im, 576, 256, $black); #axis and grid centeredtext($im, 'GMT time', 2, 30, 469, 561, 479, $black, $white, $white); $scaled = round(1000 / $scale, 0); imagestringup($im, 2, 1, 290, 'quantity * ' . $scaled, $black); centeredtext($im, '0', 2, 6, 460, 30, 470, $black, $white, $white); imageline($im, 30, 460, 561, 460, $black); imageline($im, 561, 460, 561, 40, $black); imageline($im, 561, 40, 30, 40, $black); imageline($im, 30, 40, 30, 460, $black); for ($k = 1; $k <= 24; $k++) { $cx = $k / 24 * 531 + 30; centeredtext($im, $k, 1, $cx - 4, 461, $cx + 4, 469, $black, $white, $white); if ($k != 24) { imageline($im, $cx, 460, $cx, 40, $gray); } } for ($k = 1; $k <= 10; $k++) { $cy = (10 - $k) / 10 * 420 + 40; centeredtext($im, $k, 1, 20, $cy - 4, 29, $cy + 4, $black, $white, $white);
function prepareHBarDesign() { if ($this->chart_height) { $group_width = ($this->chart_height - ($this->margin + $this->gap) * 2 - $this->header_height - $this->chart_thickness) / count($this->group_arr) - $this->bar_gap; if ($this->series_col && $this->type != "bar_mix") { $this->bar_width = $group_width / count($this->series_arr); } else { $this->bar_width = $group_width; } } else { if ($this->series_col && $this->type != "bar_mix") { $group_width = count($this->series_arr) * $this->bar_width; } else { $group_width = $this->bar_width; } $this->chart_height = ($group_width + $this->bar_gap) * count($this->group_arr) + ($this->margin + $this->gap) * 2 + $this->header_height + $this->chart_thickness; } $this->bar_height = $this->chart_width - ($this->margin + $this->gap) * 2 - $this->chart_thickness - $this->maxg_len * $this->font_wd; list($legend_width, $legend_height, $row_cnt, $col_cnt) = $this->getLegendSize(); $this->layoutData($legend_width, $legend_height); list($x, $y) = $this->drawAxis($legend_width, $legend_height); $ratio = $this->drawHAxisValue($x, $y); $c = 0; $x1 = $x; if ($this->dimension == "3d") { $x1 = $x += $this->axis_width / 2; } $y1 = $y -= $this->bar_gap / 2; foreach ($this->group_arr as $ckey => $cvalue) { $x = $x1; imagestringup($this->im, $this->label_font_size, $x - $this->font_ht - $this->axis_width * 2, $y - ($group_width - $this->font_wd * strlen($ckey)) / 2, $ckey, $this->txt_col); imagestring($this->im, $this->label_font_size, $x - $this->font_wd / 2, $y - $group_width - ($this->font_ht + $this->bar_gap) / 2, "-", $this->txt_col); $c++; $b = 0; $tval = 0; foreach ($this->series_arr as $xkey => $xvalue) { if (array_key_exists($ckey . "_" . $xkey, $this->value_arr)) { $val = $this->value_arr[$ckey . "_" . $xkey]; $x_ht = $val * $ratio; if ($this->dimension == "3d") { $dcol = $this->dseries_col_arr[$xkey]; $dcolor = imagecolorallocate($this->im, $dcol[0], $dcol[1], $dcol[2]); for ($i = $this->chart_thickness; $i > 0; $i--) { imagefilledrectangle($this->im, $x + $i, $y - $i, $x + $x_ht + $i, $y - $this->bar_width + 1 - $i, $dcolor); } } $col = $this->series_col_arr[$xkey]; $color = imagecolorallocate($this->im, $col[0], $col[1], $col[2]); imagefilledrectangle($this->im, $x, $y, $x + $x_ht, $y - $this->bar_width + 1, $color); if ($this->type == "bar_mix") { $x += $x_ht; $tval += $val; } else { $this->drawHBarLabel($val, $x, $y, $x_ht); } } if ($this->type != "bar_mix") { if ($this->series_col) { $y -= $this->bar_width; } else { if ($ckey == $xkey) { $y -= $this->bar_width; } } } $b++; } if ($this->type == "bar_mix") { $this->drawHBarLabel($tval, $x - $x_ht, $y, $x_ht); $y -= $this->bar_gap + $this->bar_width; } else { $y -= $this->bar_gap; } } $this->drawLegend($col_cnt, $row_cnt, $legend_width, $legend_height); $this->chartOutput(); }
function _StrokeBuiltinFont($x, $y, $txt, $dir = 0, $paragraph_align = "left", &$aBoundingBox, $aDebug = false) { if (is_numeric($dir) && $dir != 90 && $dir != 0) { JpGraphError::RaiseL(25091); } //(" Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead."); $h = $this->GetTextHeight($txt); $fh = $this->GetFontHeight(); $w = $this->GetTextWidth($txt); if ($this->text_halign == "right") { $x -= $dir == 0 ? $w : $h; } elseif ($this->text_halign == "center") { // For center we subtract 1 pixel since this makes the middle // be prefectly in the middle $x -= $dir == 0 ? $w / 2 - 1 : $h / 2; } if ($this->text_valign == "top") { $y += $dir == 0 ? $h : $w; } elseif ($this->text_valign == "center") { $y += $dir == 0 ? $h / 2 : $w / 2; } if ($dir == 90) { imagestringup($this->img, $this->font_family, $x, $y, $txt, $this->current_color); $aBoundingBox = array(round($x), round($y), round($x), round($y - $w), round($x + $h), round($y - $w), round($x + $h), round($y)); if ($aDebug) { // Draw bounding box $this->PushColor('green'); $this->Polygon($aBoundingBox, true); $this->PopColor(); } } else { if (ereg("\n", $txt)) { $tmp = split("\n", $txt); for ($i = 0; $i < count($tmp); ++$i) { $w1 = $this->GetTextWidth($tmp[$i]); if ($paragraph_align == "left") { imagestring($this->img, $this->font_family, $x, $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color); } elseif ($paragraph_align == "right") { imagestring($this->img, $this->font_family, $x + ($w - $w1), $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color); } else { imagestring($this->img, $this->font_family, $x + $w / 2 - $w1 / 2, $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color); } } } else { //Put the text imagestring($this->img, $this->font_family, $x, $y - $h + 1, $txt, $this->current_color); } if ($aDebug) { // Draw the bounding rectangle and the bounding box $p1 = array(round($x), round($y), round($x), round($y - $h), round($x + $w), round($y - $h), round($x + $w), round($y)); // Draw bounding box $this->PushColor('green'); $this->Polygon($p1, true); $this->PopColor(); } $aBoundingBox = array(round($x), round($y), round($x), round($y - $h), round($x + $w), round($y - $h), round($x + $w), round($y)); } }
function createAPMImage($vals, $length, $fn) { $width = 300; $height = 200; $pixelsPerSecond = $width / $length; $pic = imagecreatetruecolor($width, $height); $lineColor = imagecolorallocate($pic, 0, 0, 0); $lineColorGrey = imagecolorallocate($pic, 192, 192, 192); $bgColor = imagecolorallocate($pic, 255, 255, 255); $bgColorT = imagecolorallocatealpha($pic, 255, 255, 255, 127); imagefill($pic, 0, 0, $bgColorT); // first create x/y pairs // do this by adding up the actions of the 60 seconds before the pixel // if there are less than 60 seconds, extrapolate by multiplying with 60/$secs // the time index corresponding to a pixel can be calculated using the $pixelsPerSecond variable, // it should always be 0 < $pixelsPerSecond < 1 $xypair = array(); $maxapm = 0; for ($x = 1; $x <= $width; $x++) { $secs = ceil($x / $pixelsPerSecond); $apm = 0; if ($secs < 60) { for ($tmp = 0; $tmp < $secs; $tmp++) { if (isset($vals[$tmp])) { $apm += $vals[$tmp]; } } $apm = $apm / $secs * 60; } else { for ($tmp = $secs - 60; $tmp < $secs; $tmp++) { if (isset($vals[$tmp])) { $apm += $vals[$tmp]; } } $apm = $apm; } if ($apm > $maxapm) { $maxapm = $apm; } $xypair[$x] = $apm; } // draw the pixels if ($maxapm <= 0) { return; } for ($i = 2; $i <= $width; $i++) { imageline($pic, $i - 1, $height - $xypair[$i - 1] / $maxapm * $height, $i, $height - $xypair[$i] / $maxapm * $height, $lineColor); } // build a seperate container image $frame = imagecreatetruecolor($width + 50, $height + 50); imagefill($frame, 0, 0, $bgColor); imagerectangle($frame, 40, 0, $width + 40, $height, $lineColor); imageline($frame, 40, $height / 2, $width + 40, $height / 2, $lineColorGrey); imagestringup($frame, 4, 5, $height - 15, "APM -->", $lineColor); imagestring($frame, 4, 55, $height + 20, "Time (minutes)", $lineColor); imagestring($frame, 2, 25, $height - 15, "0", $lineColor); imagestring($frame, 2, 20, $height / 2, floor($maxapm / 2), $lineColor); imagestring($frame, 2, 20, 0, floor($maxapm), $lineColor); $lengthMins = $length / 60; for ($i = 0; $i < $lengthMins; $i += 5) { imagestring($frame, 2, 40 + $width / ($lengthMins / 5) * ($i / 5), $height + 5, $i, $lineColor); if ($i > 0) { imageline($frame, 40 + $width / ($lengthMins / 5) * ($i / 5), 0, 40 + $width / ($lengthMins / 5) * ($i / 5), $height, $lineColorGrey); } } // copy the graph onto the container image and save it imagecopy($frame, $pic, 40, 0, 0, 0, $width, $height); imagepng($frame, $fn); imagedestroy($frame); imagedestroy($pic); }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Path location of the uploaded file, with an ending slash */ function process($server_path) { $this->error = ''; $this->processed = true; if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') { $server_path = $server_path . '\\'; } } else { if (substr($server_path, -1, 1) != '/') { $server_path = $server_path . '/'; } } $this->log .= '<b>' . _("process file to") . ' ' . $server_path . '</b><br />'; // checks file size and mine type if ($this->uploaded) { if ($this->file_src_size > $this->file_max_size) { $this->processed = false; $this->error = _(MSG019); } else { $this->log .= '- ' . _("file size OK") . '<br />'; } // turn dangerous scripts into text files if ($this->no_script) { if ((substr($this->file_src_mime, 0, 5) == 'text/' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp)$/i', $this->file_src_name) || empty($this->file_src_name_ext)) { $this->file_src_mime = 'text/plain'; $this->log .= '- ' . _("script") . ' ' . $this->file_src_name . ' ' . _("renamed as") . ' ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext .= empty($this->file_src_name_ext) ? 'txt' : '.txt'; } } // checks MIME type with mime_magic if ($this->mime_magic_check && function_exists('mime_content_type')) { $detected_mime = mime_content_type($this->file_src_pathname); if ($this->file_src_mime != $detected_mime) { $this->log .= '- ' . _("MIME type detected as") . ' ' . $detected_mime . ' ' . _("but given as") . ' ' . $this->file_src_mime . '!<br />'; $this->file_src_mime = $detected_mime; } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = _("MIME type can't be detected!"); } else { if ($this->mime_check && !empty($this->file_src_mime) && !array_key_exists($this->file_src_mime, array_flip($this->allowed))) { $this->processed = false; $this->error = _("Incorrect type of file"); } else { $this->log .= '- ' . _("file mime OK") . ' : ' . $this->file_src_mime . '<br />'; } } } else { $this->error = _("File not uploaded. Can't carry on a process"); $this->processed = false; } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- ' . _("new file name body") . ' : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- ' . _("new file name ext") . ' : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a bit to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- ' . _("file name body add") . ' : ' . $this->file_name_body_add . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body); $this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body); $this->log .= '- ' . _("file name safe format") . '<br />'; } $this->log .= '- ' . _("destination variables") . '<br />'; $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />'; // do we do some image manipulation? $image_manipulation = $this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height; if ($image_manipulation) { if ($this->image_convert == '') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- ' . _("image operation, keep extension") . '<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- ' . _("image operation, change extension for conversion type") . '<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- ' . _("no image operation, keep extension") . '<br />'; } if (!$this->file_auto_rename) { $this->log .= '- ' . _("no auto_rename if same filename exists") . '<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- ' . _("checking for auto_rename") . '<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt > 1) { $this->log .= ' ' . _("auto_rename to") . ' ' . $this->file_dst_name . '<br />'; } } $this->log .= '- ' . _("destination file details") . '<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />'; if ($this->file_overwrite) { $this->log .= '- ' . _("no overwrite checking") . '<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->file_dst_name . ' ' . _("already exists. Please change the file name"); } else { $this->log .= '- ' . $this->file_dst_name . ' ' . _("doesn't exist already") . '<br />'; } } } else { $this->processed = false; } if (!$this->no_upload_check && !is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = _("No correct source file. Can't carry on a process"); } // checks if the destination directory exists, and attempt to create it if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' ' . _("doesn't exist. Attempting creation:"); if (!$this->r_mkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Destination directory can't be created. Can't carry on a process"); } else { $this->log .= ' ' . _("success") . '<br />'; } } else { $this->error = _("Destination directory doesn't exist. Can't carry on a process"); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = _("Destination path is not a directory. Can't carry on a process"); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' ' . _("is not writeable. Attempting chmod:"); if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Destination directory can't be made writeable. Can't carry on a process"); } else { $this->log .= ' ' . _("success") . '<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = _("Destination directory is still not writeable. Can't carry on a process"); } else { @fclose($f); } } } else { $this->processed = false; $this->error = _("Destination path is not a writeable. Can't carry on a process"); } } else { @fclose($f); @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } if ($this->processed) { if ($image_manipulation) { // we have a writeable destination, but we need to check if we can read the file directly // if we can't (open_basedir, etc...), we will copy it with a temporary name $has_temp_file = false; if ($this->processed && !file_exists($this->file_src_pathname)) { $this->log .= '- ' . _("can't directly access the uploaded file" . '<br />'); $this->log .= ' ' . _("attempting creating a temp file:"); $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->log .= ' ' . _("file created") . '<br />'; $this->log .= ' ' . _("temp file is:") . ' ' . $this->file_src_pathname . '<br />'; $has_temp_file = true; } else { $this->log .= ' ' . _("failed") . '<br />'; $this->processed = false; $this->error = _("Can't create the temporary file. Can't carry on a process"); } } // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = _("Source file is not readable. Can't carry on a process"); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- ' . _("image resizing or conversion wanted") . '<br />'; if ($this->gd_version()) { switch ($this->file_src_mime) { case 'image/pjpeg': case 'image/jpeg': case 'image/jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = _("No create from JPEG support"); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("Error in creating JPEG image from source"); } else { $this->log .= '- ' . _("source image is JPEG") . '<br />'; } } break; case 'image/x-png': case 'image/png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = _("No create from PNG support"); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("Error in creating PNG image from source"); } else { $this->log .= '- ' . _("source image is PNG") . '<br />'; } } break; case 'image/gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = _("Error in creating GIF image from source"); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = _("No GIF read support"); } else { $this->log .= '- ' . _("source image is GIF") . '<br />'; } } break; default: $this->processed = false; $this->error = _(MSG012); } } else { $this->processed = false; $this->error = _("GD doesn't seem to be present"); } if ($this->processed && $image_src) { $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; $gd_version = $this->gd_version(); $ratio_crop = null; if ($this->image_resize) { $this->log .= '- ' . _("resizing...") . '<br />'; if ($this->image_ratio_x) { $this->log .= ' ' . _("calculate x size") . '<br />'; $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y); $this->image_dst_y = $this->image_y; } else { if ($this->image_ratio_y) { $this->log .= ' ' . _("calculate y size") . '<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x); } else { if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' ' . _("check x/y sizes") . '<br />'; if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) { $this->image_ratio_crop = ''; } $this->image_ratio_crop = strtolower($this->image_ratio_crop); if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ' . _("ratio_crop_x") . ' : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ' . _("ratio_crop_y") . ' : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); } } } else { $this->log .= ' ' . _("doesn't calculate x/y sizes") . '<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' ' . _("use plain sizes") . '<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; } } } if ($this->preserve_transparency && $this->file_src_mime != 'image/gif' && $this->file_src_mime != 'image/png') { $this->preserve_transparency = false; } if ($gd_version >= 2 && !$this->preserve_transparency) { $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); } else { $image_dst = imagecreate($this->image_dst_x, $this->image_dst_y); } if ($this->preserve_transparency) { $this->log .= '- ' . _("preserve transparency") . '<br />'; $transparent_color = imagecolortransparent($image_src); imagepalettecopy($image_dst, $image_src); imagefill($image_dst, 0, 0, $transparent_color); imagecolortransparent($image_dst, $transparent_color); } if ($gd_version >= 2 && !$this->preserve_transparency) { $res = imagecopyresampled($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($image_dst, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } $this->log .= ' ' . _("resized image object created") . '<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />'; } else { // we only convert, so we link the dst image to the src image $image_dst =& $image_src; } // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= ' ' . _("setting destination file type to") . ' ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; } // crop imag (and also crops if image_ratio_crop is used) if ($gd_version >= 2 && (!empty($this->image_crop) || !is_null($ratio_crop))) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) { $ct += $ratio_crop['t']; } if (array_key_exists('r', $ratio_crop)) { $cr += $ratio_crop['r']; } if (array_key_exists('b', $ratio_crop)) { $cb += $ratio_crop['b']; } if (array_key_exists('l', $ratio_crop)) { $cl += $ratio_crop['l']; } } $this->log .= '- ' . _("crop image") . ' : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y); // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- ' . _("flip image") . ' : ' . $this->image_flip . '<br />'; $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) { $this->image_rotate = 0; } if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp = imagecreatetruecolor($this->image_dst_y, $this->image_dst_x); } else { $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); } $this->log .= '- ' . _("rotate image") . ' : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } } // add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && !empty($this->image_overlay_color))) { $this->log .= '- ' . _("apply color overlay") . '<br />'; sscanf($this->image_overlay_color, "#%2x%2x%2x", $red, $green, $blue); $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); imagecopymerge($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); } // add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- ' . _("apply tint, light, contrast correction, negative, greyscale and threshold") . '<br />'; if (!empty($this->image_tint_color)) { sscanf($this->image_tint_color, "#%2x%2x%2x", $red, $green, $blue); } $background = imagecolorallocatealpha($image_dst, 255, 255, 255, 0); imagefill($image_dst, 0, 0, $background); imagealphablending($image_dst, TRUE); for ($y = 0; $y < $this->image_dst_y; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_threshold)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127; $r = $g = $b = $c > $this->image_threshold ? 255 : 0; $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_brightness)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0); $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0); $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (is_numeric($this->image_contrast)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0); $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0); $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (!empty($this->image_tint_color)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = min(round($red * $pixel['red'] / 169), 255); $g = min(round($green * $pixel['green'] / 169), 255); $b = min(round($blue * $pixel['blue'] / 169), 255); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } if (!empty($this->image_negative)) { $rgb = imagecolorat($image_dst, $x, $y); $pixel = imagecolorsforindex($image_dst, $rgb); $r = round(255 - $pixel['red']); $g = round(255 - $pixel['green']); $b = round(255 - $pixel['blue']); $a = $pixel['alpha']; $pixelcolor = imagecolorallocatealpha($image_dst, $r, $g, $b, $a); imagesetpixel($image_dst, $x, $y, $pixelcolor); } } } } // adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- ' . _("add border") . ' : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- ' . _("add border") . ' : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) { sscanf($this->image_border_color, "#%2x%2x%2x", $red, $green, $blue); } $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefill($tmp, 0, 0, $background); imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- ' . _("add frame") . ' : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- ' . _("add frame") . ' : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); } $nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + $nb * 2; $this->image_dst_y = $this->image_dst_y + $nb * 2; $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2); for ($i = 0; $i < $nb; $i++) { sscanf($vars[$i], "#%2x%2x%2x", $red, $green, $blue); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } } // we transfert tmp into image_dst imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } // add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) { $this->image_bevel_color1 = '#FFFFFF'; } if (empty($this->image_bevel_color2)) { $this->image_bevel_color2 = '#000000'; } sscanf($this->image_bevel_color1, "#%2x%2x%2x", $red1, $green1, $blue1); sscanf($this->image_bevel_color2, "#%2x%2x%2x", $red2, $green2, $blue2); imagealphablending($image_dst, true); for ($i = 0; $i < $this->image_bevel; $i++) { $alpha = round($i / $this->image_bevel * 127); $c1 = imagecolorallocatealpha($image_dst, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($image_dst, $red2, $green2, $blue2, $alpha); imageline($image_dst, $i, $i, $this->image_dst_x - $i - 1, $i, $c1); imageline($image_dst, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2); imageline($image_dst, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2); imageline($image_dst, $i, $i, $i, $this->image_dst_y - $i - 1, $c1); } } // add watermark image if ($this->image_watermark != '' && file_exists($this->image_watermark)) { $this->log .= '- ' . _("add watermark") . '<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : NULL; // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false; if ($watermark_type == 1) { if (!function_exists('imagecreatefromgif')) { $this->error = _("No create from GIF support, can't read watermark"); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = _("No GIF read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is GIF") . '<br />'; $watermark_checked = true; } } } else { if ($watermark_type == 2) { if (!function_exists('imagecreatefromjpeg')) { $this->error = _("No create from JPG support, can't read watermark"); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = _("No JPG read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is JPG") . '<br />'; $watermark_checked = true; } } } else { if ($watermark_type == 3) { if (!function_exists('imagecreatefrompng')) { $this->error = _("No create from PNG support, can't read watermark"); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = _("No PNG read support, can't create watermark"); } else { $this->log .= ' ' . _("watermark source image is PNG") . '<br />'; $watermark_checked = true; } } } } } if ($watermark_checked) { $watermark_width = imagesx($filter); $watermark_height = imagesy($filter); $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_width; } else { if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_width) / 2; } } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_height; } else { if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_height) / 2; } } } imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height); } else { $this->error = _("Watermark image is of unknown type"); } } // add text if (!empty($this->image_text)) { $this->log .= '- ' . _("add text") . '<br />'; if (!is_numeric($this->image_text_padding)) { $this->image_text_padding = 0; } if (!is_numeric($this->image_text_line_spacing)) { $this->image_text_line_spacing = 0; } if (!is_numeric($this->image_text_padding_x)) { $this->image_text_padding_x = $this->image_text_padding; } if (!is_numeric($this->image_text_padding_y)) { $this->image_text_padding_y = $this->image_text_padding; } $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' ' . _("try to load font") . ' ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= _("success") . '<br />'; } else { $this->log .= _("error") . '<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = ImageFontWidth($this->image_text_font); $char_height = ImageFontHeight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = $char_width * strlen($v); if ($h > $text_height) { $text_height = $h; } $line_width = $char_height; $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } else { $w = $char_width * strlen($v); if ($w > $text_width) { $text_width = $w; } $line_height = $char_height; $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } } $text_width += 2 * $this->image_text_padding_x; $text_height += 2 * $this->image_text_padding_y; $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else { if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else { if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } } // add a background, maybe transparent if (!empty($this->image_text_background)) { sscanf($this->image_text_background, "#%2x%2x%2x", $red, $green, $blue); if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter = imagecreatetruecolor($text_width, $text_height); $background_color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); imagecopymerge($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imageColorAllocate($image_dst, $red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } } $text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - 2 * $this->image_text_padding_x; $t_height = $text_height - 2 * $this->image_text_padding_y; sscanf($this->image_text_color, "#%2x%2x%2x", $red, $green, $blue); // add the text, maybe transparent if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) { $t_width = 0; } if ($t_height < 0) { $t_height = 0; } $filter = imagecreatetruecolor($t_width, $t_height); $color = imagecolorallocate($filter, 0, 0, $blue == 0 ? 255 : 0); imagefill($filter, 0, 0, $color); $text_color = imagecolorallocate($filter, $red, $green, $blue); imagecolortransparent($filter, $color); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } imagecopymerge($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } } } // add a reflection if ($this->image_reflection_height) { $this->log .= '- ' . _("add reflection") . ' : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%') > 0) { $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100); } if (strpos($image_reflection_height, 'px') > 0) { $image_reflection_height = str_replace('px', '', $image_reflection_height); } $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) { $image_reflection_height = $this->image_dst_y; } if (empty($this->image_reflection_color)) { $this->image_reflection_color = '#FFFFFF'; } if (empty($this->image_reflection_opacity)) { $this->image_reflection_opacity = 60; } // create the new destination image $tmp = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space); sscanf($this->image_reflection_color, "#%2x%2x%2x", $red, $green, $blue); $color = imagecolorallocate($tmp, $red, $green, $blue); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, $color); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // copy the reflection for ($y = 0; $y < $image_reflection_height; $y++) { imagecopymerge($tmp, $image_dst, 0, $y + $this->image_dst_y + $this->image_reflection_space, 0, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0), $this->image_dst_x, 1, (int) $transparency); if ($transparency > 0) { $transparency = $transparency - $this->image_reflection_opacity / $image_reflection_height; } } // copy the resulting image into the destination image $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space; imagedestroy($image_dst); $image_dst = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); imagecopy($image_dst, $tmp, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagedestroy($tmp); } if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) { // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net $this->log .= '- ' . _("JPEG desired file size") . ' : ' . $this->jpeg_size . '<br />'; //calculate size of each image. 75%, 50%, and 25% quality ob_start(); imagejpeg($image_dst, '', 75); $buffer = ob_get_contents(); ob_end_clean(); $size75 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 50); $buffer = ob_get_contents(); ob_end_clean(); $size50 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 25); $buffer = ob_get_contents(); ob_end_clean(); $size25 = strlen($buffer); //calculate gradient of size reduction by quality $mgrad1 = 25 / ($size50 - $size25); $mgrad2 = 25 / ($size75 - $size50); $mgrad3 = 50 / ($size75 - $size25); $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3; //result of approx. quality factor for expected size $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50); if ($q_factor < 1) { $this->jpeg_quality = 1; } elseif ($q_factor > 100) { $this->jpeg_quality = 100; } else { $this->jpeg_quality = $q_factor; } $this->log .= ' ' . _("JPEG quality factor set to") . ' ' . $this->jpeg_quality . '<br />'; } // outputs image $this->log .= '- ' . _("converting..") . '<br />'; switch ($this->image_convert) { case 'jpeg': case 'jpg': $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality); if (!$result) { $this->processed = false; $this->error = _("No JPEG create support"); } else { $this->log .= ' ' . _("JPEG image created") . '<br />'; } break; case 'png': $result = @imagepng($image_dst, $this->file_dst_pathname); if (!$result) { $this->processed = false; $this->error = _("No PNG create support"); } else { $this->log .= ' ' . _("PNG image created") . '<br />'; } break; case 'gif': $result = @imagegif($image_dst, $this->file_dst_pathname); if (!$result) { $this->processed = false; $this->error = _("No GIF create support"); } else { $this->log .= ' ' . _("GIF image created") . '<br />'; } break; default: $this->processed = false; $this->error = _("No convertion type defined"); } if ($this->processed) { if (is_resource($image_src)) { imagedestroy($image_src); } if (is_resource($image_dst)) { imagedestroy($image_dst); } $this->log .= ' ' . _("image objects destroyed") . '<br />'; } } if ($has_temp_file) { $this->log .= '- ' . _("deletes temporary file") . '<br />'; @unlink($this->file_src_pathname); } } else { $this->log .= '- ' . _("no image processing wanted") . '<br />'; if (!$this->no_upload_check) { $result = is_uploaded_file($this->file_src_pathname); } else { $result = TRUE; } if ($result) { if (!$this->no_upload_check) { if (!move_uploaded_file($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = _("Error copying file on the server. move_uploaded_file() failed"); } } else { if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = _("Error copying file on the server. copy() failed"); } } } else { $this->processed = false; $this->error = _("Error copying file on the server. Incorrect source file"); } } } if ($this->processed) { $this->log .= '- <b>' . _("process OK") . '</b><br />'; } // we reinit all the var $this->init(); }
/** * Draw X and Y axes * * @return @e void */ protected function _drawAxes() { //----------------------------------------- // Allocate text and shadow cols //----------------------------------------- $textcolor = imagecolorallocate($this->image, hexdec(substr($this->options['titlecolor'], 1, 2)), hexdec(substr($this->options['titlecolor'], 3, 2)), hexdec(substr($this->options['titlecolor'], 5, 2))); $shadowcolor = imagecolorallocate($this->image, hexdec(substr($this->options['titleshadow'], 1, 2)), hexdec(substr($this->options['titleshadow'], 3, 2)), hexdec(substr($this->options['titleshadow'], 5, 2))); //----------------------------------------- // Do we have axes titles? //----------------------------------------- if ($this->options['xaxistitle']) { if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $this->options['xaxistitle']); $textx = round(($this->grapharea['x1'] - $this->grapharea['x0']) / 2, 0) - round(($txtsize[4] - $txtsize[0]) / 2, 0); $texty = $this->grapharea['y1']; imagettftext($this->image, 10, 0, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $this->options['xaxistitle']); imagettftext($this->image, 10, 0, $textx, $texty, $textcolor, $this->options['font'], $this->options['xaxistitle']); $this->grapharea['y1'] = $this->grapharea['y1'] - ($txtsize[1] - $txtsize[5]) - 5; } else { $txtwidth = imagefontwidth($this->fontsize) * strlen($this->options['xaxistitle']); $textx = round(($this->grapharea['x1'] - $this->grapharea['x0']) / 2, 0) - round($txtwidth / 2, 0); $texty = $this->grapharea['y1'] - imagefontheight($this->fontsize); imagestring($this->image, $this->fontsize, $textx + 1, $texty + 1, $this->options['xaxistitle'], $shadowcolor); imagestring($this->image, $this->fontsize, $textx, $texty, $this->options['xaxistitle'], $textcolor); $this->grapharea['y1'] = $this->grapharea['y1'] - imagefontheight($this->fontsize) - 5; } } if ($this->options['yaxistitle']) { if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $this->options['yaxistitle']); $textx = $this->grapharea['x0'] + ($txtsize[1] - $txtsize[5]); $texty = round(($this->grapharea['y1'] - $this->grapharea['y0']) / 2, 0) + ($txtsize[4] - $txtsize[0]); imagettftext($this->image, 10, 90, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $this->options['yaxistitle']); imagettftext($this->image, 10, 90, $textx, $texty, $textcolor, $this->options['font'], $this->options['yaxistitle']); $this->grapharea['x0'] = $textx + 5; } else { $txtheight = imagefontwidth($this->fontsize) * strlen($this->options['yaxistitle']); $textx = $this->grapharea['x0']; $texty = round(($this->grapharea['y1'] - $this->grapharea['y0']) / 2, 0) + round($txtheight / 2, 0); imagestringup($this->image, $this->fontsize, $textx + 1, $texty + 1, $this->options['yaxistitle'], $shadowcolor); imagestringup($this->image, $this->fontsize, $textx, $texty, $this->options['yaxistitle'], $textcolor); $this->grapharea['x0'] = $this->grapharea['x0'] + imagefontheight($this->fontsize) + 5; } } //----------------------------------------- // Determine height of the x-axis //----------------------------------------- $xaxisheight = 0; if (isset($this->x_axis['type'])) { if ($this->x_axis['type'] == 'numeric') { if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $this->x_axis['max']); $xaxisheight = $txtsize[1] - $txtsize[5]; } else { $xaxisheight = imagefontheight($this->fontsize); } } else { $xaxisheight = 0; foreach ($this->x_axis['labels'] as $label) { if ($this->use_ttf) { $textsize = imagettfbbox(10, 45, $this->options['font'], $label); if ($textsize[1] - $textsize[5] > $xaxisheight) { $xaxisheight = $textsize[1] - $textsize[5]; } } else { $textsize = imagefontwidth($this->fontsize) * strlen($label); if ($textsize > $xaxisheight) { $xaxisheight = $textsize; } } } } $xaxisheight += 8; $this->grapharea['y1'] -= $xaxisheight; } //----------------------------------------- // Determine width of the y-axis //----------------------------------------- $yaxiswidth = 0; if (isset($this->y_axis['type'])) { if ($this->y_axis['type'] == 'numeric') { if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $this->y_axis['max']); $yaxiswidth = $txtsize[2] - $txtsize[0]; } else { $yaxiswidth = imagefontwidth($this->fontsize) * strlen($this->y_axis['max']); } } else { $yaxiswidth = 0; foreach ($this->y_axis['labels'] as $label) { if ($this->use_ttf) { $textsize = imagettfbbox(10, 0, $this->options['font'], $label); if ($textsize[2] - $textsize[0] > $yaxiswidth) { $yaxiswidth = $textsize[2] - $textsize[0]; } } else { $textsize = imagefontwidth($this->fontsize) * strlen($label); if ($textsize > $yaxiswidth) { $yaxiswidth = $textsize; } } } } $yaxiswidth += 8; $this->grapharea['x0'] += $yaxiswidth; } //----------------------------------------- // Determine 3D effect size (different for different graph types) //----------------------------------------- $effect3DSize = 0; $numdepth = 1; if ($this->options['style3D'] == 1) { if ($this->options['charttype'] == 'Line') { $effect3DSize = 20; $numdepth = count($this->data['yaxis']); } elseif ($this->options['charttype'] == 'Area') { $effect3DSize = 20; } elseif ($this->options['charttype'] == 'HBar') { //calculate 1 bar height $numbars = count($this->data['yaxis'][0]['data']); $stepsize = round(($this->grapharea['y1'] - $this->grapharea['y0'] - 20) / $numbars * 0.9, 0); $barheight = round($stepsize / count($this->data['yaxis']), 0); $effect3DSize = round($barheight / 2, 0) < 20 ? round($barheight / 2, 0) : 20; } elseif ($this->options['charttype'] == 'Bar') { //calculate 1 bar width $numbars = count($this->data['yaxis'][0]['data']); $stepsize = round(($this->grapharea['x1'] - $this->grapharea['x0'] - 20) / $numbars * 0.9, 0); $barwidth = round($stepsize / count($this->data['yaxis']), 0); $effect3DSize = round($barwidth / 2, 0) < 20 ? round($barwidth / 2, 0) : 20; } elseif ($this->options['charttype'] == 'Funnel') { $effect3DSize = 40; } } $this->grapharea['x1'] -= $effect3DSize * $numdepth; $this->grapharea['y0'] += $effect3DSize * $numdepth; $depth = $numdepth * $effect3DSize; //----------------------------------------- // Draw x-axis ticks & labels //----------------------------------------- if (isset($this->x_axis['type'])) { if ($this->y_axis['type'] == 'labels') { $numlabels = count($this->y_axis['labels']); $stepheight = floor(($this->grapharea['y1'] - $this->grapharea['y0']) / $numlabels); $this->grapharea['y0'] = $this->grapharea['y1'] - $numlabels * $stepheight; } if ($this->x_axis['type'] == 'numeric') { // 0 - Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], '0'); $textx = $this->grapharea['x0'] - round(($txtsize[2] - $txtsize[0]) / 2, 0); $texty = $txtsize[1] - $txtsize[5] + $this->grapharea['y1'] + 8; imagettftext($this->image, 10, 0, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], '0'); imagettftext($this->image, 10, 0, $textx, $texty, $textcolor, $this->options['font'], '0'); } else { $txtwidth = imagefontwidth($this->fontsize) * strlen('0'); $textx = $this->grapharea['x0'] - round($txtwidth / 2, 0); $texty = $this->grapharea['y1'] + 8; imagestring($this->image, $this->fontsize, $textx + 1, $texty + 1, '0', $shadowcolor); imagestring($this->image, $this->fontsize, $textx, $texty, '0', $textcolor); } $numticks = $this->options['numticks'] > $this->x_axis['max'] ? floor($this->x_axis['max']) : $this->options['numticks']; $step = floor($this->x_axis['max'] / $numticks); $steps = ceil($this->x_axis['max'] / $step); $stepwidth = ($this->grapharea['x1'] - $this->grapharea['x0']) / $steps; for ($i = 1; $i <= $steps; $i++) { $value = $i * $step; $x = round($value / $step * $stepwidth + $this->grapharea['x0'], 0); // Ticks imageline($this->image, $x, $this->grapharea['y1'], $x, $this->grapharea['y1'] + 5, $this->black); if ($depth) { imageline($this->image, $x, $this->grapharea['y1'], $x + $depth, $this->grapharea['y1'] - $depth, $this->black); } if ($this->options['showgridlinesx']) { imageline($this->image, $x + $depth, $this->grapharea['y1'] - $depth, $x + $depth, $this->grapharea['y0'] - $depth, $this->black); } // Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $value); $textx = $x - round(($txtsize[2] - $txtsize[0]) / 2, 0); $texty = $txtsize[1] - $txtsize[5] + $this->grapharea['y1'] + 8; imagettftext($this->image, 10, 0, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $value); imagettftext($this->image, 10, 0, $textx, $texty, $textcolor, $this->options['font'], $value); } else { $txtwidth = imagefontwidth($this->fontsize) * strlen($value); $textx = $x - round($txtwidth / 2, 0); $texty = $this->grapharea['y1'] + 8; imagestring($this->image, $this->fontsize, $textx + 1, $texty + 1, $value, $shadowcolor); imagestring($this->image, $this->fontsize, $textx, $texty, $value, $textcolor); } } } elseif ($this->x_axis['type'] == 'ticklabels') { $numlabels = count($this->x_axis['labels']); $stepwidth = floor(($this->grapharea['x1'] - $this->grapharea['x0']) / ($numlabels - 1)); $this->grapharea['x1'] = $this->grapharea['x0'] + ($numlabels - 1) * $stepwidth; for ($i = 0; $i < $numlabels; $i++) { $label = $this->x_axis['labels'][$i]; $x = $i * $stepwidth + $this->grapharea['x0']; // Ticks imageline($this->image, $x, $this->grapharea['y1'], $x, $this->grapharea['y1'] + 5, $this->black); if ($depth) { imageline($this->image, $x, $this->grapharea['y1'], $x + $depth, $this->grapharea['y1'] - $depth, $this->black); } if ($this->options['showgridlinesx']) { imageline($this->image, $x + $depth, $this->grapharea['y1'] - $depth, $x + $depth, $this->grapharea['y0'] - $depth, $this->black); } // Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 45, $this->options['font'], $label); $textx = $x - ($txtsize[4] - $txtsize[0]); $texty = $txtsize[1] - $txtsize[5] + $this->grapharea['y1'] + 8; imagettftext($this->image, 10, 45, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $label); imagettftext($this->image, 10, 45, $textx, $texty, $textcolor, $this->options['font'], $label); } else { $textx = $x - round(imagefontheight($this->fontsize) / 2, 0); $texty = $this->grapharea['y1'] + 8 + imagefontwidth($this->fontsize) * strlen($label); imagestringup($this->image, $this->fontsize, $textx + 1, $texty + 1, $label, $shadowcolor); imagestringup($this->image, $this->fontsize, $textx, $texty, $label, $this->black); } } } else { $numlabels = count($this->x_axis['labels']); $stepwidth = floor(($this->grapharea['x1'] - $this->grapharea['x0']) / $numlabels); $this->grapharea['x1'] = $this->grapharea['x0'] + $numlabels * $stepwidth; for ($i = 0; $i < $numlabels; $i++) { $label = $this->x_axis['labels'][$i]; $x = ($i + 1) * $stepwidth + $this->grapharea['x0']; // Ticks imageline($this->image, $x, $this->grapharea['y1'], $x, $this->grapharea['y1'] + 5, $this->black); if ($depth) { imageline($this->image, $x, $this->grapharea['y1'], $x + $depth, $this->grapharea['y1'] - $depth, $this->black); } if ($this->options['showgridlinesx']) { imageline($this->image, $x + $depth, $this->grapharea['y1'] - $depth, $x + $depth, $this->grapharea['y0'] - $depth, $this->black); } if ($this->options['xaxisskip']) { if ($i % $this->options['xaxisskip'] != 1) { continue; } } // Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 45, $this->options['font'], $label); $textx = $x - round($stepwidth / 2, 0) - ($txtsize[4] - $txtsize[0]); $texty = $txtsize[1] - $txtsize[5] + $this->grapharea['y1'] + 8; imagettftext($this->image, 10, 45, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $label); imagettftext($this->image, 10, 45, $textx, $texty, $textcolor, $this->options['font'], $label); } else { $textx = $x - round($stepwidth / 2, 0) - round(imagefontheight($this->fontsize) / 2, 0); $texty = $this->grapharea['y1'] + 8 + imagefontwidth($this->fontsize) * strlen($label); imagestringup($this->image, $this->fontsize, $textx + 1, $texty + 1, $label, $shadowcolor); imagestringup($this->image, $this->fontsize, $textx, $texty, $label, $this->black); } } } } //----------------------------------------- // Draw y-axis ticks & labels //----------------------------------------- if (isset($this->y_axis['type'])) { if ($this->y_axis['type'] == 'numeric') { // 0 - Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], '0'); $textx = $this->grapharea['x0'] - ($txtsize[2] - $txtsize[0]) - 8; $texty = $this->grapharea['y1'] + round(($txtsize[1] - $txtsize[5]) / 2, 0); imagettftext($this->image, 10, 0, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], '0'); imagettftext($this->image, 10, 0, $textx, $texty, $textcolor, $this->options['font'], '0'); } else { $txtwidth = imagefontwidth($this->fontsize) * strlen('0'); $textx = $this->grapharea['x0'] - $txtwidth - 8; $texty = $this->grapharea['y1'] - round(imagefontheight($this->fontsize) / 2, 0); imagestring($this->image, $this->fontsize, $textx + 1, $texty + 1, '0', $shadowcolor); imagestring($this->image, $this->fontsize, $textx, $texty, '0', $textcolor); } $numticks = $this->options['numticks'] > $this->y_axis['max'] ? floor($this->y_axis['max']) : $this->options['numticks']; $numticks = $numticks ? $numticks : 1; $step = floor($this->y_axis['max'] / $numticks); $step = $step ? $step : 1; $steps = ceil($this->y_axis['max'] / $step); $steps = $steps ? $steps : 1; $stepheight = ($this->grapharea['y1'] - $this->grapharea['y0']) / $steps; for ($i = 1; $i <= $steps; $i++) { $value = $i * $step; $y = round($this->grapharea['y1'] - $value / $step * $stepheight, 0); // Ticks imageline($this->image, $this->grapharea['x0'], $y, $this->grapharea['x0'] - 5, $y, $this->black); if ($depth) { imageline($this->image, $this->grapharea['x0'], $y, $this->grapharea['x0'] + $depth, $y - $depth, $this->black); } if ($this->options['showgridlinesy']) { imageline($this->image, $this->grapharea['x0'] + $depth, $y - $depth, $this->grapharea['x1'] + $depth, $y - $depth, $this->black); } // Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $value); $textx = $this->grapharea['x0'] - ($txtsize[2] - $txtsize[0]) - 8; $texty = $y + round(($txtsize[1] - $txtsize[5]) / 2, 0); imagettftext($this->image, 10, 0, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $value); imagettftext($this->image, 10, 0, $textx, $texty, $textcolor, $this->options['font'], $value); } else { $txtwidth = imagefontwidth($this->fontsize) * strlen($value); $textx = $this->grapharea['x0'] - $txtwidth - 8; $texty = $y - round(imagefontheight($this->fontsize) / 2, 0); imagestring($this->image, $this->fontsize, $textx + 1, $texty + 1, $value, $shadowcolor); imagestring($this->image, $this->fontsize, $textx, $texty, $value, $textcolor); } } } else { $numlabels = count($this->y_axis['labels']); $stepheight = floor(($this->grapharea['y1'] - $this->grapharea['y0']) / $numlabels); $this->grapharea['y0'] = $this->grapharea['y1'] - $numlabels * $stepheight; for ($i = 0; $i < $numlabels; $i++) { $label = $this->y_axis['labels'][$i]; $y = $this->grapharea['y1'] - ($i + 1) * $stepheight; // Ticks imageline($this->image, $this->grapharea['x0'], $y, $this->grapharea['x0'] - 5, $y, $this->black); if ($depth) { imageline($this->image, $this->grapharea['x0'], $y, $this->grapharea['x0'] + $depth, $y - $depth, $this->black); } if ($this->options['showgridlinesy']) { imageline($this->image, $this->grapharea['x0'] + $depth, $y - $depth, $this->grapharea['x1'] + $depth, $y - $depth, $this->black); } // Labels if ($this->use_ttf) { $txtsize = imagettfbbox(10, 0, $this->options['font'], $label); $textx = $this->grapharea['x0'] - ($txtsize[4] - $txtsize[0]) - 8; $texty = $y + round($stepheight / 2, 0); imagettftext($this->image, 10, 0, $textx + 1, $texty + 1, $shadowcolor, $this->options['font'], $label); imagettftext($this->image, 10, 0, $textx, $texty, $textcolor, $this->options['font'], $label); } else { $texty = $y + round($stepheight / 2, 0) - round(imagefontheight($this->fontsize) / 2, 0); $textx = $this->grapharea['x0'] - imagefontwidth($this->fontsize) * strlen($label) - 8; imagestring($this->image, $this->fontsize, $textx + 1, $texty + 1, $label, $shadowcolor); imagestring($this->image, $this->fontsize, $textx, $texty, $label, $this->black); } } } } //----------------------------------------- // Draw the lines //----------------------------------------- imageline($this->image, $this->grapharea['x0'] - 5, $this->grapharea['y1'], $this->grapharea['x1'], $this->grapharea['y1'], $this->black); imageline($this->image, $this->grapharea['x0'], $this->grapharea['y0'], $this->grapharea['x0'], $this->grapharea['y1'] + 5, $this->black); if ($effect3DSize) { for ($i = 1; $i <= $numdepth; $i++) { $depth = $effect3DSize * $i; imageline($this->image, $this->grapharea['x0'], $this->grapharea['y1'], $this->grapharea['x0'] + $depth, $this->grapharea['y1'] - $depth, $this->black); imageline($this->image, $this->grapharea['x0'] + $depth, $this->grapharea['y1'] - $depth, $this->grapharea['x0'] + $depth, $this->grapharea['y0'] - $depth, $this->black); imageline($this->image, $this->grapharea['x0'] + $depth, $this->grapharea['y0'] - $depth, $this->grapharea['x0'], $this->grapharea['y0'], $this->black); imageline($this->image, $this->grapharea['x0'] + $depth, $this->grapharea['y1'] - $depth, $this->grapharea['x1'] + $depth, $this->grapharea['y1'] - $depth, $this->black); imageline($this->image, $this->grapharea['x1'] + $depth, $this->grapharea['y1'] - $depth, $this->grapharea['x1'], $this->grapharea['y1'], $this->black); } } }
function StrokeText($x, $y, $txt, $dir = 0) { if (!is_numeric($dir)) { die("JpGraph Error: Direction for text most be given as an angle between 0 and 90."); } if ($this->font_family >= FONT0 && $this->font_family <= FONT2_BOLD) { // Internal font if (is_numeric($dir) && $dir != 90 && $dir != 0) { die("JpGraph Error: Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead."); } if (($this->font_family == FONT1 || $this->font_family == FONT2) && $this->font_style == FS_BOLD) { ++$this->font_family; } $h = $this->GetFontHeight($txt); $w = $this->GetTextWidth($txt); if ($this->text_halign == "right") { $x -= $dir == 0 ? $w : $h; } elseif ($this->text_halign == "center") { $x -= $dir == 0 ? $w / 2 : $h / 2; } if ($this->text_valign == "top") { $y += $dir == 0 ? $h : $w; } elseif ($this->text_valign == "center") { $y += $dir == 0 ? $h / 2 : $w / 2; } if ($dir == 90) { imagestringup($this->img, $this->font_family, $x, $y, $txt, $this->current_color); } else { imagestring($this->img, $this->font_family, $x, $y - $h + 1, $txt, $this->current_color); } } else { // TTF font $file = $this->ttf->File($this->font_family, $this->font_style); //echo "FF = $this->font_family, file=$file"; $angle = $dir; $bbox = ImageTTFBBox($this->font_size, $angle, $file, $txt); if ($this->text_halign == "right") { $x -= $bbox[2] - $bbox[0]; } elseif ($this->text_halign == "center") { $x -= ($bbox[4] - $bbox[0]) / 2; } elseif ($this->text_halign == "topanchor") { $x -= $bbox[4] - $bbox[0]; } elseif ($this->text_halign == "left") { $x += -($bbox[6] - $bbox[0]); } if ($this->text_valign == "top") { $y -= $bbox[5]; } elseif ($this->text_valign == "center") { $y -= ($bbox[5] - $bbox[1]) / 2; } elseif ($this->text_valign == "bottom") { $y -= $bbox[1]; } // Use lower left of bbox as fix-point, not the default baselinepoint. // $x -= $bbox[0]; ImageTTFText($this->img, $this->font_size, $angle, $x, $y, $this->current_color, $file, $txt); } }
/** * Add a text on the background image of the layer using a default font registered in GD. * * @param string $text * @param int $font * @param string $color * @param int $positionX * @param int $positionY * @param string $align */ public function writeText($text, $font = 1, $color = 'ffffff', $positionX = 0, $positionY = 0, $align = 'horizontal') { $RGBTextColor = ImageWorkshopLib::convertHexToRGB($color); $textColor = imagecolorallocate($this->image, $RGBTextColor['R'], $RGBTextColor['G'], $RGBTextColor['B']); if ($align == 'horizontal') { imagestring($this->image, $font, $positionX, $positionY, $text, $textColor); } else { imagestringup($this->image, $font, $positionX, $positionY, $text, $textColor); } }
public function writestrup($str) { // add by new network http://necz.net $b = imagecolorallocate($this->workingImage, 0, 0, 0); $w = imagecolorallocate($this->workingImage, 255, 255, 255); $len = strlen($str) * 6; $wa = imagesx($this->workingImage) - 14; imagestringup($this->workingImage, 2, $wa + 1, $len, $str, $b); imagestringup($this->workingImage, 2, $wa - 1, $len, $str, $b); imagestringup($this->workingImage, 2, $wa, $len + 1, $str, $b); imagestringup($this->workingImage, 2, $wa, $len - 1, $str, $b); imagestringup($this->workingImage, 2, $wa, $len, $str, $w); $this->oldImage = $this->workingImage; return $this; }
function _StrokeBuiltinFont($x, $y, $txt, $dir = 0, $paragraph_align = "left") { if (is_numeric($dir) && $dir != 90 && $dir != 0) { JpGraphError::Raise(" Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead."); } $h = $this->GetTextHeight($txt); $fh = $this->GetFontHeight(); $w = $this->GetTextWidth($txt); if ($this->text_halign == "right") { $x -= $dir == 0 ? $w : $h; } elseif ($this->text_halign == "center") { // For center we subtract 1 pixel since this makes the middle // be prefectly in the middle $x -= $dir == 0 ? $w / 2 - 1 : $h / 2; } if ($this->text_valign == "top") { $y += $dir == 0 ? $h : $w; } elseif ($this->text_valign == "center") { $y += $dir == 0 ? $h / 2 : $w / 2; } if ($dir == 90) { imagestringup($this->img, $this->font_family, $x, $y, $txt, $this->current_color); } else { if (ereg("\n", $txt)) { $tmp = split("\n", $txt); for ($i = 0; $i < count($tmp); ++$i) { $w1 = $this->GetTextWidth($tmp[$i]); if ($paragraph_align == "left") { imagestring($this->img, $this->font_family, $x, $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color); } elseif ($paragraph_align == "right") { imagestring($this->img, $this->font_family, $x + ($w - $w1), $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color); } else { imagestring($this->img, $this->font_family, $x + $w / 2 - $w1 / 2, $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color); } } } else { //Put the text imagestring($this->img, $this->font_family, $x, $y - $h + 1, $txt, $this->current_color); } } }
function setpixeloneup(&$image, $px, $py) { $img = imagecreatetruecolor(100, 100); $red = imagecolorallocate($img, 255, 0, 0); $i = 0; $j = 0; for ($j = 0; $j < 5; $j++) { for ($i = 0; $i < 5; $i++) { imagesetpixel($image, $px + $i, $py + $j, $red); } } imagestringup($image, 2, $px - 5, $py - 5, $px . "x" . $py, $red); }
function get_histogram($data, $x_legend, $y_legend, $x_tick_gap) { $img_dim_x = 800; $img_dim_y = 500; $im = @imagecreate($img_dim_x, $img_dim_y) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 255, 255, 255); // white $black = imagecolorallocate($im, 0, 0, 0); #black $blue = imagecolorallocate($im, 0, 0, 255); #blue $x1 = 20; $y1 = 10; $x2 = $img_dim_x - 10; $y2 = $img_dim_y - 40; imagerectangle($im, $x1, $y1, $x2, $y2, $black); $x_axis_legend_x = ($x1 + $x2) / 2 - 30; $x_axis_legend_y = $y2 + 25; imagestring($im, 3, $x_axis_legend_x, $x_axis_legend_y, $x_legend, $black); $y_axis_legend_x = $x1 - 15; $y_axis_legend_y = ($y1 + $y2) / 2; imagestringup($im, 3, $y_axis_legend_x, $y_axis_legend_y, $y_legend, $black); $keys = array_keys($data); $values = array_values($data); sort($keys, SORT_NUMERIC); $max = max($values); $min = min($values); for ($i = 0; $i < count($keys); $i++) { $height = 0.9 * ($y2 - $y1) * $data[$keys[$i]] / $max; $bar_x1 = ($x2 - $x1) / count($keys) * ($i + 0.2) + $x1; $bar_y1 = $y2 - $height; $bar_x2 = ($x2 - $x1) / count($keys) * ($i + 0.8) + $x1; $bar_y2 = $y2; imagefilledrectangle($im, $bar_x1, $bar_y1, $bar_x2, $bar_y2, $blue); # imagestring($im,3,$bar_x1,$bar_y1-15,$data[$keys[$i]],$black); if ($i % $x_tick_gap == 0) { imagestring($im, 3, $bar_x1, $bar_y2 + 5, $keys[$i], $black); } } for ($i = 0; $i < count($keys); $i++) { $height = 0.9 * ($y2 - $y1) * $data[$keys[$i]] / $max; $bar_x1 = ($x2 - $x1) / count($keys) * ($i + 0.2) + $x1; $bar_y1 = $y2 - $height; $bar_x2 = ($x2 - $x1) / count($keys) * ($i + 0.8) + $x1; $bar_y2 = $y2; imagestring($im, 3, $bar_x1, $bar_y1 - 15, $data[$keys[$i]], $black); } return $im; }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * If the function is called with a null or empty argument, then it will return the content of the picture * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Optional path location of the uploaded file, with an ending slash * @return string Optional content of the image */ function process($server_path = null) { $this->error = ''; $this->processed = true; $return_mode = false; $return_content = null; // clean up dst variables $this->file_dst_path = ''; $this->file_dst_pathname = ''; $this->file_dst_name = ''; $this->file_dst_name_body = ''; $this->file_dst_name_ext = ''; if (!$this->uploaded) { $this->error = $this->translate('file_not_uploaded'); $this->processed = false; } if ($this->processed) { if (empty($server_path) || is_null($server_path)) { $this->log .= '<b>process file and return the content</b><br />'; $return_mode = true; } else { if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') { $server_path = $server_path . '\\'; } } else { if (substr($server_path, -1, 1) != '/') { $server_path = $server_path . '/'; } } $this->log .= '<b>process file to ' . $server_path . '</b><br />'; } } if ($this->processed) { // checks file max size if ($this->file_src_size > $this->file_max_size) { $this->processed = false; $this->error = $this->translate('file_too_big'); } else { $this->log .= '- file size OK<br />'; } } if ($this->processed) { // if we have an image without extension, set it if ($this->file_is_image && !$this->file_src_name_ext_) { $this->file_src_name_ext = $this->file_src_name_ext_ = $this->image_src_type; } // turn dangerous scripts into text files if ($this->no_script) { if ((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name) || empty($this->file_src_name_ext_)) { $this->file_src_mime = 'text/plain'; $this->log .= '- script ' . $this->file_src_name . ' renamed as ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext = $this->file_src_name_ext_ . (empty($this->file_src_name_ext_) ? 'txt' : '.txt'); } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = $this->translate('no_mime'); } else { if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) { list($m1, $m2) = explode('/', $this->file_src_mime); $allowed = false; // check wether the mime type is allowed foreach ($this->allowed as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = true; break; } } // check wether the mime type is forbidden foreach ($this->forbidden as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = false; break; } } if (!$allowed) { $this->processed = false; $this->error = $this->translate('incorrect_file'); } else { $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />'; } } else { $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />'; } } // if the file is an image, we can check on its dimensions // these checks are not available if open_basedir restrictions are in place if ($this->file_is_image) { if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) { $ratio = $this->image_src_x / $this->image_src_y; if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) { $this->processed = false; $this->error = $this->translate('image_too_wide'); } if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) { $this->processed = false; $this->error = $this->translate('image_too_narrow'); } if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) { $this->processed = false; $this->error = $this->translate('image_too_high'); } if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) { $this->processed = false; $this->error = $this->translate('image_too_short'); } if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_high'); } if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_low'); } if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) { $this->processed = false; $this->error = $this->translate('too_many_pixels'); } if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) { $this->processed = false; $this->error = $this->translate('not_enough_pixels'); } } else { $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />'; } } } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->file_overwrite) { $this->file_auto_rename = false; } if ($this->image_convert != '') { // if we convert as an image $this->file_dst_name_ext = $this->image_convert; $this->log .= '- new file name ext : ' . $this->image_convert . '<br />'; } if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a string to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />'; } if ($this->file_name_body_pre != '') { // prepend a string to the name $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body; $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body); $this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body); $this->log .= '- file name safe format<br />'; } $this->log .= '- destination variables<br />'; if (empty($this->file_dst_path) || is_null($this->file_dst_path)) { $this->log .= ' file_dst_path : n/a<br />'; } else { $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; } $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />'; // do we do some image manipulation? $image_manipulation = $this->file_is_image && ($this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || $this->image_unsharp || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_precrop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height); if ($image_manipulation) { if ($this->image_convert == '') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- image operation, keep extension<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- image operation, change extension for conversion type<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- no image operation, keep extension<br />'; } if (!$return_mode) { if (!$this->file_auto_rename) { $this->log .= '- no auto_rename if same filename exists<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- checking for auto_rename<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt > 1) { $this->log .= ' auto_rename to ' . $this->file_dst_name . '<br />'; } } $this->log .= '- destination file details<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />'; if ($this->file_overwrite) { $this->log .= '- no overwrite checking<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('already_exists', array($this->file_dst_name)); } else { $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />'; } } } } if ($this->processed) { // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists if (!empty($this->file_src_temp)) { $this->log .= '- use the temp file instead of the original file since it is a second process<br />'; $this->file_src_pathname = $this->file_src_temp; if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('temp_file_missing'); } // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file() } else { if (!$this->no_upload_check) { if (!is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists() } else { if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } } } // checks if the destination directory exists, and attempt to create it if (!$return_mode) { if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:'; if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir'); } else { $this->log .= ' success<br />'; } } else { $this->error = $this->translate('destination_dir_missing'); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = $this->translate('destination_path_not_dir'); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:'; if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { $this->log .= ' success<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { @fclose($f); } } } else { $this->processed = false; $this->error = $this->translate('destination_path_write'); } } else { if ($this->processed) { @fclose($f); } @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction) // then we create a temp file that will be used as the source file in subsequent processes // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists) if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) { $this->log .= '- attempting to use a temp file:'; $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->file_src_temp = $this->file_src_pathname; $this->log .= ' file created<br />'; $this->log .= ' temp file is: ' . $this->file_src_temp . '<br />'; } else { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('temp_file'); } } } } if ($this->processed) { // we do a quick check to ensure the file is really an image // we can do this only now, as it would have failed before in case of open_basedir if ($image_manipulation && !@getimagesize($this->file_src_pathname)) { $this->log .= '- the file is not an image!<br />'; $image_manipulation = false; } if ($image_manipulation) { // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = $this->translate('source_not_readable'); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- image resizing or conversion wanted<br />'; if ($this->gdversion()) { switch ($this->image_src_type) { case 'jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('JPEG')); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('JPEG')); } else { $this->log .= '- source image is JPEG<br />'; } } break; case 'png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('PNG')); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('PNG')); } else { $this->log .= '- source image is PNG<br />'; } } break; case 'gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('GIF')); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('GIF')); } else { $this->log .= '- source image is GIF<br />'; } } break; case 'bmp': if (!method_exists($this, 'imagecreatefrombmp')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('BMP')); } else { $image_src = @$this->imagecreatefrombmp($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('BMP')); } else { $this->log .= '- source image is BMP<br />'; } } break; default: $this->processed = false; $this->error = $this->translate('source_invalid'); } } else { $this->processed = false; $this->error = $this->translate('gd_missing'); } if ($this->processed && $image_src) { // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= '- setting destination file type to ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; } if (!in_array($this->image_convert, $this->image_supported)) { $this->image_convert = 'jpg'; } // we set the default color to be the background color if we don't output in a transparent format if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) { $this->image_background_color = $this->image_default_color; } if (!empty($this->image_background_color)) { $this->image_default_color = $this->image_background_color; } if (empty($this->image_default_color)) { $this->image_default_color = '#FFFFFF'; } $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $gd_version = $this->gdversion(); $ratio_crop = null; if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif' $this->log .= '- image is detected as having a palette<br />'; $this->image_is_palette = true; $this->image_transparent_color = imagecolortransparent($image_src); if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) { $this->image_is_transparent = true; $this->log .= ' palette image is detected as transparent<br />'; } // if the image has a palette (GIF), we convert it to true color, preserving transparency $this->log .= ' convert palette image to true color<br />'; $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y); imagealphablending($true_color, false); imagesavealpha($true_color, true); for ($x = 0; $x < $this->image_src_x; $x++) { for ($y = 0; $y < $this->image_src_y; $y++) { if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) { imagesetpixel($true_color, $x, $y, 127 << 24); } else { $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y)); imagesetpixel($true_color, $x, $y, $rgb['alpha'] << 24 | $rgb['red'] << 16 | $rgb['green'] << 8 | $rgb['blue']); } } } $image_src = $this->imagetransfer($true_color, $image_src); imagealphablending($image_src, false); imagesavealpha($image_src, true); $this->image_is_palette = false; } $image_dst =& $image_src; // pre-crop image, before resizing if (!empty($this->image_precrop)) { if (is_array($this->image_precrop)) { $vars = $this->image_precrop; } else { $vars = explode(' ', $this->image_precrop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_src_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_src_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_src_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_src_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_src_x = $this->image_src_x - $cl - $cr; $this->image_src_y = $this->image_src_y - $ct - $cb; if ($this->image_src_x < 1) { $this->image_src_x = 1; } if ($this->image_src_y < 1) { $this->image_src_y = 1; } $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y) if ($this->image_resize) { $this->log .= '- resizing...<br />'; if ($this->image_ratio_x) { $this->log .= ' calculate x size<br />'; $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y); $this->image_dst_y = $this->image_y; } else { if ($this->image_ratio_y) { $this->log .= ' calculate y size<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x); } else { if (is_numeric($this->image_ratio_pixels)) { $this->log .= ' calculate x/y size to match a number of pixels<br />'; $pixels = $this->image_src_y * $this->image_src_x; $diff = sqrt($this->image_ratio_pixels / $pixels); $this->image_dst_x = round($this->image_src_x * $diff); $this->image_dst_y = round($this->image_src_y * $diff); } else { if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' check x/y sizes<br />'; if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) { $this->image_ratio_crop = ''; } $this->image_ratio_crop = strtolower($this->image_ratio_crop); if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_ratio_fill) { if (!is_string($this->image_ratio_fill)) { $this->image_ratio_fill = ''; } $this->image_ratio_fill = strtolower($this->image_ratio_fill); if ($this->image_src_x / $this->image_x < $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_fill, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_fill, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_fill, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_fill, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); } } } } else { $this->log .= ' doesn\'t calculate x/y sizes<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' use plain sizes<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; } } } } if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); if ($gd_version >= 2) { $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } $this->log .= ' resized image object created<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />'; // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } else { $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } // crop image (and also crops if image_ratio_crop is used) if (!empty($this->image_crop) || !is_null($ratio_crop)) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) { $ct += $ratio_crop['t']; } if (array_key_exists('r', $ratio_crop)) { $cr += $ratio_crop['r']; } if (array_key_exists('b', $ratio_crop)) { $cb += $ratio_crop['b']; } if (array_key_exists('l', $ratio_crop)) { $cl += $ratio_crop['l']; } } $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct - 1, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl - 1, $this->image_dst_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- flip image : ' . $this->image_flip . '<br />'; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) { $this->image_rotate = 0; } if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x); } else { $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); } $this->log .= '- rotate image : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } } // unsharp mask if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) { // Unsharp Mask for PHP - version 2.1.1 // Unsharp mask algorithm by Torstein Hønsi 2003-07. // Used with permission // Modified to support alpha transparency if ($this->image_unsharp_amount > 500) { $this->image_unsharp_amount = 500; } $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016; if ($this->image_unsharp_radius > 50) { $this->image_unsharp_radius = 50; } $this->image_unsharp_radius = $this->image_unsharp_radius * 2; if ($this->image_unsharp_threshold > 255) { $this->image_unsharp_threshold = 255; } $this->image_unsharp_radius = abs(round($this->image_unsharp_radius)); if ($this->image_unsharp_radius != 0) { $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst); $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); if (function_exists('imageconvolution')) { // PHP >= 5.1 $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imageconvolution($blur, $matrix, 16, 0); } else { for ($i = 0; $i < $this->image_unsharp_radius; $i++) { imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333); // up $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down } } $p_new = array(); if ($this->image_unsharp_threshold > 0) { for ($x = 0; $x < $this->image_dst_x - 1; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red'])) : $p_orig['red']; $p_new['green'] = abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green'])) : $p_orig['green']; $p_new['blue'] = abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue'])) : $p_orig['blue']; if ($p_orig['red'] != $p_new['red'] || $p_orig['green'] != $p_new['green'] || $p_orig['blue'] != $p_new['blue']) { $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } else { for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red']; if ($p_new['red'] > 255) { $p_new['red'] = 255; } elseif ($p_new['red'] < 0) { $p_new['red'] = 0; } $p_new['green'] = $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green']; if ($p_new['green'] > 255) { $p_new['green'] = 255; } elseif ($p_new['green'] < 0) { $p_new['green'] = 0; } $p_new['blue'] = $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue']; if ($p_new['blue'] > 255) { $p_new['blue'] = 255; } elseif ($p_new['blue'] < 0) { $p_new['blue'] = 0; } $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } imagedestroy($canvas); imagedestroy($blur); } } // add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && $this->image_overlay_percent > 0 && !empty($this->image_overlay_color))) { $this->log .= '- apply color overlay<br />'; list($red, $green, $blue) = $this->getcolors($this->image_overlay_color); $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); } // add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />'; if (!empty($this->image_tint_color)) { list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color); } imagealphablending($image_dst, true); for ($y = 0; $y < $this->image_dst_y; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_threshold)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127; $r = $g = $b = $c > $this->image_threshold ? 255 : 0; $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_brightness)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0); $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0); $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_contrast)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0); $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0); $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_tint_color)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = min(round($tint_red * $pixel['red'] / 169), 255); $g = min(round($tint_green * $pixel['green'] / 169), 255); $b = min(round($tint_blue * $pixel['blue'] / 169), 255); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_negative)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = round(255 - $pixel['red']); $g = round(255 - $pixel['green']); $b = round(255 - $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } // adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- add border : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- add border : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) { list($red, $green, $blue) = $this->getcolors($this->image_border_color); } // we now create an image, that we fill with the border color $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background); // we then copy the source image into the new image, without merging so that only the border is actually kept imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); } $nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + $nb * 2; $this->image_dst_y = $this->image_dst_y + $nb * 2; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2); for ($i = 0; $i < $nb; $i++) { list($red, $green, $blue) = $this->getcolors($vars[$i]); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) { $this->image_bevel_color1 = '#FFFFFF'; } if (empty($this->image_bevel_color2)) { $this->image_bevel_color2 = '#000000'; } list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1); list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2); $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagealphablending($tmp, true); for ($i = 0; $i < $this->image_bevel; $i++) { $alpha = round($i / $this->image_bevel * 127); $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha); imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c1); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c1); } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add watermark image if ($this->image_watermark != '' && file_exists($this->image_watermark)) { $this->log .= '- add watermark<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : null; // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false; if ($watermark_type == IMAGETYPE_GIF) { if (!function_exists('imagecreatefromgif')) { $this->error = $this->translate('watermark_no_create_support', array('GIF')); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('GIF')); } else { $this->log .= ' watermark source image is GIF<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_JPEG) { if (!function_exists('imagecreatefromjpeg')) { $this->error = $this->translate('watermark_no_create_support', array('JPEG')); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('JPEG')); } else { $this->log .= ' watermark source image is JPEG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_PNG) { if (!function_exists('imagecreatefrompng')) { $this->error = $this->translate('watermark_no_create_support', array('PNG')); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('PNG')); } else { $this->log .= ' watermark source image is PNG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_BMP) { if (!method_exists($this, 'imagecreatefrombmp')) { $this->error = $this->translate('watermark_no_create_support', array('BMP')); } else { $filter = @$this->imagecreatefrombmp($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('BMP')); } else { $this->log .= ' watermark source image is BMP<br />'; $watermark_checked = true; } } } else { $this->error = $this->translate('watermark_invalid'); } } } } if ($watermark_checked) { $watermark_dst_width = $watermark_src_width = imagesx($filter); $watermark_dst_height = $watermark_src_height = imagesy($filter); // if watermark is too large/tall, resize it first if (!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y) || !$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y) { $canvas_width = $this->image_dst_x - abs($this->image_watermark_x); $canvas_height = $this->image_dst_y - abs($this->image_watermark_y); if ($watermark_src_width / $canvas_width > $watermark_src_height / $canvas_height) { $watermark_dst_width = $canvas_width; $watermark_dst_height = intval($watermark_src_height * ($canvas_width / $watermark_src_width)); } else { $watermark_dst_height = $canvas_height; $watermark_dst_width = intval($watermark_src_width * ($canvas_height / $watermark_src_height)); } $this->log .= ' watermark resized from ' . $watermark_src_width . 'x' . $watermark_src_height . ' to ' . $watermark_dst_width . 'x' . $watermark_dst_height . '<br />'; } // determine watermark position $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_dst_width; } else { if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2; } } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_dst_height; } else { if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2; } } } imagealphablending($image_dst, true); imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height); } else { $this->error = $this->translate('watermark_invalid'); } } // add text if (!empty($this->image_text)) { $this->log .= '- add text<br />'; // calculate sizes in human readable format $src_size = $this->file_src_size / 1024; $src_size_mb = number_format($src_size / 1024, 1, ".", " "); $src_size_kb = number_format($src_size, 1, ".", " "); $src_size_human = $src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb"; $this->image_text = str_replace(array('[src_name]', '[src_name_body]', '[src_name_ext]', '[src_pathname]', '[src_mime]', '[src_size]', '[src_size_kb]', '[src_size_mb]', '[src_size_human]', '[src_x]', '[src_y]', '[src_pixels]', '[src_type]', '[src_bits]', '[dst_path]', '[dst_name_body]', '[dst_name_ext]', '[dst_name]', '[dst_pathname]', '[dst_x]', '[dst_y]', '[date]', '[time]', '[host]', '[server]', '[ip]', '[gd_version]'), array($this->file_src_name, $this->file_src_name_body, $this->file_src_name_ext, $this->file_src_pathname, $this->file_src_mime, $this->file_src_size, $src_size_kb, $src_size_mb, $src_size_human, $this->image_src_x, $this->image_src_y, $this->image_src_pixels, $this->image_src_type, $this->image_src_bits, $this->file_dst_path, $this->file_dst_name_body, $this->file_dst_name_ext, $this->file_dst_name, $this->file_dst_pathname, $this->image_dst_x, $this->image_dst_y, date('Y-m-d'), date('H:i:s'), isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a', isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a', isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a', $this->gdversion(true)), $this->image_text); if (!is_numeric($this->image_text_padding)) { $this->image_text_padding = 0; } if (!is_numeric($this->image_text_line_spacing)) { $this->image_text_line_spacing = 0; } if (!is_numeric($this->image_text_padding_x)) { $this->image_text_padding_x = $this->image_text_padding; } if (!is_numeric($this->image_text_padding_y)) { $this->image_text_padding_y = $this->image_text_padding; } $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' try to load font ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= 'success<br />'; } else { $this->log .= 'error<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = imagefontwidth($this->image_text_font); $char_height = imagefontheight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = $char_width * strlen($v); if ($h > $text_height) { $text_height = $h; } $line_width = $char_height; $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } else { $w = $char_width * strlen($v); if ($w > $text_width) { $text_width = $w; } $line_height = $char_height; $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } } $text_width += 2 * $this->image_text_padding_x; $text_height += 2 * $this->image_text_padding_y; $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else { if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else { if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } } // add a background, maybe transparent if (!empty($this->image_text_background)) { list($red, $green, $blue) = $this->getcolors($this->image_text_background); if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter = imagecreatetruecolor($text_width, $text_height); $background_color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imagecolorallocate($image_dst, $red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } } $text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - 2 * $this->image_text_padding_x; $t_height = $text_height - 2 * $this->image_text_padding_y; list($red, $green, $blue) = $this->getcolors($this->image_text_color); // add the text, maybe transparent if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) { $t_width = 0; } if ($t_height < 0) { $t_height = 0; } $filter = $this->imagecreatenew($t_width, $t_height, false, true); $text_color = imagecolorallocate($filter, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } } } // add a reflection if ($this->image_reflection_height) { $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%') > 0) { $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100); } if (strpos($image_reflection_height, 'px') > 0) { $image_reflection_height = str_replace('px', '', $image_reflection_height); } $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) { $image_reflection_height = $this->image_dst_y; } if (empty($this->image_reflection_opacity)) { $this->image_reflection_opacity = 60; } // create the new destination image $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // we have to make sure the extra bit is the right color, or transparent if ($image_reflection_height + $this->image_reflection_space > 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fill in from the edge of the extra bit imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill); } // copy the reflection for ($y = 0; $y < $image_reflection_height; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space)); $pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0))); $alpha_o = 1 - $pixel_o['alpha'] / 127; $alpha_b = 1 - $pixel_b['alpha'] / 127; $opacity = $alpha_o * $transparency / 100; if ($opacity > 0) { $red = round(($pixel_o['red'] * $opacity + $pixel_b['red'] * $alpha_b) / ($alpha_b + $opacity)); $green = round(($pixel_o['green'] * $opacity + $pixel_b['green'] * $alpha_b) / ($alpha_b + $opacity)); $blue = round(($pixel_o['blue'] * $opacity + $pixel_b['blue'] * $alpha_b) / ($alpha_b + $opacity)); $alpha = $opacity + $alpha_b; if ($alpha > 1) { $alpha = 1; } $alpha = round((1 - $alpha) * 127); $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha); imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color); } } if ($transparency > 0) { $transparency = $transparency - $this->image_reflection_opacity / $image_reflection_height; } } // copy the resulting image into the destination image $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space; $image_dst = $this->imagetransfer($tmp, $image_dst); } // reduce the JPEG image to a set desired size if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) { // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />'; // calculate size of each image. 75%, 50%, and 25% quality ob_start(); imagejpeg($image_dst, '', 75); $buffer = ob_get_contents(); ob_end_clean(); $size75 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 50); $buffer = ob_get_contents(); ob_end_clean(); $size50 = strlen($buffer); ob_start(); imagejpeg($image_dst, '', 25); $buffer = ob_get_contents(); ob_end_clean(); $size25 = strlen($buffer); // calculate gradient of size reduction by quality $mgrad1 = 25 / ($size50 - $size25); $mgrad2 = 25 / ($size75 - $size50); $mgrad3 = 50 / ($size75 - $size25); $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3; // result of approx. quality factor for expected size $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50); if ($q_factor < 1) { $this->jpeg_quality = 1; } elseif ($q_factor > 100) { $this->jpeg_quality = 100; } else { $this->jpeg_quality = $q_factor; } $this->log .= ' JPEG quality factor set to ' . $this->jpeg_quality . '<br />'; } // converts image from true color, and fix transparency if needed $this->log .= '- converting...<br />'; switch ($this->image_convert) { case 'gif': // if the image is true color, we convert it to a palette if (imageistruecolor($image_dst)) { $this->log .= ' true color to palette<br />'; // creates a black and white mask $mask = array(array()); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $mask[$x][$y] = $pixel['alpha']; } } list($red, $green, $blue) = $this->getcolors($this->image_default_color); // first, we merge the image with the background color, so we know which colors we will have for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($mask[$x][$y] > 0) { // we have some transparency. we combine the color with the default color $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $alpha = $mask[$x][$y] / 127; $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha); $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha); $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha); $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } // transforms the true color image into palette, with its merged default color if (empty($this->image_background_color)) { imagetruecolortopalette($image_dst, true, 255); $transparency = imagecolorallocate($image_dst, 254, 1, 253); imagecolortransparent($image_dst, $transparency); // make the transparent areas transparent for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { // we test wether we have enough opacity to justify keeping the color if ($mask[$x][$y] > 120) { imagesetpixel($image_dst, $x, $y, $transparency); } } } } unset($mask); } break; case 'jpg': case 'bmp': // if the image doesn't support any transparency, then we merge it with the default color $this->log .= ' fills in transparency with default color<br />'; list($red, $green, $blue) = $this->getcolors($this->image_default_color); $transparency = imagecolorallocate($image_dst, $red, $green, $blue); // make the transaparent areas transparent for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { // we test wether we have some transparency, in which case we will merge the colors if (imageistruecolor($image_dst)) { $rgba = imagecolorat($image_dst, $x, $y); $pixel = array('red' => $rgba >> 16 & 0xff, 'green' => $rgba >> 8 & 0xff, 'blue' => $rgba & 0xff, 'alpha' => ($rgba & 0x7f000000) >> 24); } else { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); } if ($pixel['alpha'] == 127) { // we have full transparency. we make the pixel transparent imagesetpixel($image_dst, $x, $y, $transparency); } else { if ($pixel['alpha'] > 0) { // we have some transparency. we combine the color with the default color $alpha = $pixel['alpha'] / 127; $pixel['red'] = round($pixel['red'] * (1 - $alpha) + $red * $alpha); $pixel['green'] = round($pixel['green'] * (1 - $alpha) + $green * $alpha); $pixel['blue'] = round($pixel['blue'] * (1 - $alpha) + $blue * $alpha); $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } } break; default: break; } // outputs image $this->log .= '- saving image...<br />'; switch ($this->image_convert) { case 'jpeg': case 'jpg': if (!$return_mode) { $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality); } else { ob_start(); $result = @imagejpeg($image_dst, '', $this->jpeg_quality); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('JPEG')); } else { $this->log .= ' JPEG image created<br />'; } break; case 'png': imagealphablending($image_dst, false); imagesavealpha($image_dst, true); if (!$return_mode) { $result = @imagepng($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagepng($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('PNG')); } else { $this->log .= ' PNG image created<br />'; } break; case 'gif': if (!$return_mode) { $result = @imagegif($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagegif($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('GIF')); } else { $this->log .= ' GIF image created<br />'; } break; case 'bmp': if (!$return_mode) { $result = $this->imagebmp($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = $this->imagebmp($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('BMP')); } else { $this->log .= ' BMP image created<br />'; } break; default: $this->processed = false; $this->error = $this->translate('no_conversion_type'); } if ($this->processed) { if (is_resource($image_src)) { imagedestroy($image_src); } if (is_resource($image_dst)) { imagedestroy($image_dst); } $this->log .= ' image objects destroyed<br />'; } } } else { $this->log .= '- no image processing wanted<br />'; if (!$return_mode) { // copy the file to its final destination. we don't use move_uploaded_file here // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('copy_failed'); } } else { // returns the file, so that its content can be received by the caller $return_content = @file_get_contents($this->file_src_pathname); if ($return_content === FALSE) { $this->processed = false; $this->error = $this->translate('reading_failed'); } } } } if ($this->processed) { $this->log .= '- <b>process OK</b><br />'; } else { $this->log .= '- <b>error</b>: ' . $this->error . '<br />'; } // we reinit all the vars $this->init(); // we may return the image content if ($return_mode) { return $return_content; } }
$yt = $sizeY * $true[$i - 1] / 100; if ($yt > 0) { imagefilledrectangle($im, $x1 + $shiftX, $shiftYup, $x1 + $shiftX + 8, $yt + $shiftYup, imagecolorallocate($im, 235, 120, 120)); // red } $yf = $sizeY * $false[$i - 1] / 100; if ($yf > 0) { imagefilledrectangle($im, $x1 + $shiftX, $yt + $shiftYup, $x1 + $shiftX + 8, $sizeY + $shiftYup, imagecolorallocate($im, 120, 235, 120)); // green } if ($yt + $yf > 0) { imagerectangle($im, $x1 + $shiftX, $shiftYup, $x1 + $shiftX + 8, $sizeY + $shiftYup, $black); } } for ($i = 0; $i <= $sizeY; $i += $sizeY / 10) { imageText($im, 7, 0, $sizeX + 5 + $shiftX, $sizeY - $i - 4 + $shiftYup + 8, $darkred, $i * ($maxY - $minY) / $sizeY + $minY); } imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 39, $shiftX + 5, $sizeY + $shiftYup + 44, imagecolorallocate($im, 120, 235, 120)); imagerectangle($im, $shiftX, $sizeY + $shiftYup + 39, $shiftX + 5, $sizeY + $shiftYup + 44, $black); imageText($im, 8, 0, $shiftX + 9, $sizeY + $shiftYup + 45, $black, _('OK') . ' (%)'); imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 54, $shiftX + 5, $sizeY + $shiftYup + 59, imagecolorallocate($im, 235, 120, 120)); imagerectangle($im, $shiftX, $sizeY + $shiftYup + 54, $shiftX + 5, $sizeY + $shiftYup + 59, $black); imageText($im, 8, 0, $shiftX + 9, $sizeY + $shiftYup + 60, $black, _('Problems') . ' (%)'); imagestringup($im, 1, imagesx($im) - 10, imagesy($im) - 50, ZABBIX_HOMEPAGE, $gray); $str = sprintf('%0.2f', microtime(true) - $startTime); $str = _s('Generated in %s sec', $str); $strSize = imageTextSize(6, 0, $str); imageText($im, 6, 0, imagesx($im) - $strSize['width'] - 5, imagesy($im) - 5, $gray, $str); imageOut($im); imagedestroy($im); require_once dirname(__FILE__) . '/include/page_footer.php';