$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;
}
$image_dst = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
if ($gd_version >= 2) {
$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;
}
// crop imag (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)) {
sscanf($this->image_background_color, "#%2x%2x%2x", $red, $green, $blue);
$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, $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, $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);
}