= $old_radio) { $new_width = floor($new_height * $old_width / $old_height); } else { $new_height = floor($new_width * $old_height / $old_width); } $image_p = imagecreatetruecolor($new_width, $new_height); if ($type == 1) $image = imagecreatefromgif($file); if ($type == 2) $image = imagecreatefromjpeg($file); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); if ($type == 1) imagegif($image_p, $file, 100); if ($type == 2) imagejpeg($image_p, $file,100); } } function cropCentered($file, $new_width = 0, $new_height = 0) { list($old_width, $old_height, $type) = getimagesize($file); $tmp_image = imagecreatetruecolor(50, 50); if ($type == 1) $image = imagecreatefromgif($file); if ($type == 2) $image = imagecreatefromjpeg($file); imagecopyresampled($tmp_image, $image, 0, 0, 0, 0, 500, 500, $old_width, $old_height); if ($type == 1) imagegif($tmp_image, $file, 100); if ($type == 2) imagejpeg($tmp_image, $file,100); } function cropImage($nw, $nh, $source, $dest) { $size = getimagesize($source); $w = $size[0]; $h = $size[1]; $stype = $size[2]; switch($stype) { case 1: $simg = imagecreatefromgif($source); break; case 2: $simg = imagecreatefromjpeg($source); break; case 3: $simg = imagecreatefrompng($source); break; } if ($nw || $nh) { if ($nw == false) { $nw = $w * $nh/$h; } elseif ($nh == false) { $nh = $h * $nw/$w; } $wm = $w/$nw; $hm = $h/$nh; $h_height = $nh/2; $w_height = $nw/2; if ($nw<=$w && $nh <= $h) { $dimg = imagecreatetruecolor($nw, $nh); /*if($nw < $nh) { $adjusted_width = ($wm < $hm) ? $w / $wm : $w / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); } elseif(($nw > $nh) || ($nw == $nh)) { */ $adjusted_width = ($wm < $hm) ? $w / $wm + 1 : $w / $hm + 1; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; $adjusted_height = ($wm > $hm) ? $h / $hm + 1 : $h / $wm + 1; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($dimg,$simg,-$int_width,-$int_height,0,0,$adjusted_width,$adjusted_height,$w,$h); /* } else { imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); }*/ } else { $dimg = imagecreatetruecolor($w, $h); imagecopyresampled($dimg,$simg,0,0,0,0,$w,$h,$w,$h); } } else { $dimg = imagecreatetruecolor($w, $h); imagecopyresampled($dimg,$simg,0,0,0,0,$w,$h,$w,$h); } imagejpeg($dimg,$dest,100); } if (isset($_GET['i'])) { $file = UPLOAD_DIR.$_GET['i']; if (file_exists($file)) { if (!isset($_GET['w'])) { $width = false; } else { $width = $_GET['w']; } if (!isset($_GET['h'])) { $height = false; } else { $height = $_GET['h']; } if ($width < 10000 && $height < 10000) { // tikrinam ar failas egzistuoja ir modifikacijos laika $create_new = false; if ( file_exists($file."_".$width.'x'.$height) && (filemtime($file) > filemtime($file."_".$width.'x'.$height) ) ) { $create_new = true; unlink($file."_".$width.'x'.$height); } elseif ( file_exists($file."_".$width.'x'.$height) && (filemtime($file) < filemtime($file."_".$width.'x'.$height) ) ){ $file = $file."_".$width.'x'.$height; } else { $create_new = true; } //kuriam nauja faila jei reikia if ($create_new == true) { if (copy($file, $file."_".$width.'x'.$height)) { $file = $file."_".$width.'x'.$height; chmod($file, 0777); cropImage($width, $height, $file, $file) ; } else { die('Negaliu kopijuoti.'); } } } header("Content-Type: image/jpeg"); $image = imagecreatefromjpeg($file); imagejpeg($image, "", 100); } else die("Nerasta"); } ?>