Untitled

 avatar
unknown
php
4 years ago
5.3 kB
5
Indexable
function crop($src, $xwidth, $xheight, $noprint="", $expath="cache"){

    $export_path = $expath;

	$src = AP.'/noduploads/'.$src;

	if (!file_exists($src) or is_dir($src)) {
		//return false;
		echo "";
		return;
	} elseif (($size = getimagesize ( $src )) === false) {
		//return false;
		echo "";
		return;
	}

	/*
	if (! function_exists ( $icfunc = 'imagecreatefrom' . strtolower ( substr ( $size ['mime'], strpos ( $size ['mime'], '/' ) + 1 ) ) )) {
		//return false;
		echo "";
		return;
	}*/

	$icfunc = "imagecreatefromjpeg";

	$filename = end ( explode ( '/', $src ) );
	$ext = end ( explode ( '.', $filename ) );

	$original_aspect = $size[0] / $size[1];
	$thumb_aspect = $xwidth / $xheight;

	if ( $original_aspect >= $thumb_aspect ){
		// If image is wider than thumbnail (in aspect ratio sense)
		$new_height = $xheight;
		$new_width = $size[0] / ($size[1] / $xheight);
	}else{
		// If the thumbnail is wider than the image
		$new_width = $xwidth;
		$new_height = $size[1] / ($size[0] / $xwidth);
	}

	$modtime = filemtime($src);
	if($newfilename==""){
		$_filename = md5($src.$modtime).'_'.$xwidth.'_'.$xheight.'.'.$ext;
	}else{
		$_filename = md5($src.$modtime).'_'.$newfilename.'.'.$ext;
	}

	if (file_exists(AP.'/'.$export_path.'/'.$_filename) && substr($_filename, 0, strpos($_filename, '_')) == md5($src.$modtime)) {
		
		//$idest = $icfunc ( 'cache/'.$_filename );
		if($noprint==""){
		    
		    echo root."/".$export_path."/".$_filename;
		    
		}else{
		    
		    return root."/".$export_path."/".$_filename;
		    
		}
		
	} else {

		$image = imagecreatefromjpeg($src);
		$thumb = imagecreatetruecolor( $xwidth, $xheight );

		// Resize and crop
		imagecopyresampled($thumb,
				$image,
				0 - ($new_width - $xwidth) / 2, // Center the image horizontally
				0 - ($new_height - $xheight) / 2, // Center the image vertically
				0, 0,
				$new_width, $new_height,
				$size[0], $size[1]);
		imagejpeg($thumb, AP.'/'.$export_path.'/'.$_filename, QUALITY);

		imagedestroy ( $thumb );
		imagedestroy ( $image );
		
		if($noprint==""){
		    
		    echo root."/".$export_path."/".$_filename;
		    
		}else{
		    
		    return root."/".$export_path."/".$_filename;
		    
		}

	}

}




function resim2($xwidth, $src, $wm="", $xheight="", $newfilename="", $noprint="", $expath="cache"){

    $export_path = $expath;

	$src = AP.'/noduploads/'.$src;

	if (!file_exists($src) or is_dir($src)) {
		//return false;
		echo "";
		return;
	} elseif (($size = getimagesize ( $src )) === false) {
		//return false;
		echo "";
		return;
	}

	/*
	 if (!function_exists ( $icfunc = 'imagecreatefrom' . strtolower ( substr ( $size ['mime'], strpos ( $size ['mime'], '/' ) + 1 ) ) )) {
	//return false;
	echo "";
	return;
	}
	*/

	$icfunc = "imagecreatefromjpeg";

	$filename = end ( explode ( '/', $src ) );
	$ext = end ( explode ( '.', $filename ) );

	//$fsize = filesize($src);

	if($size[0]>=$size[1]){
		if($size[0] <= $xwidth){
			$width = $size[0];
			$height = $size[1];
		}else{
			$width = (int) $xwidth;
			$height = (int) @$xheight;
		}
	}else{

		if($size[1] <= $xheight){
			$width = $size[0];
			$height = $size[1];
		}else{
			if($xheight == 0 or $xheight == ""){
				if($size[0]<$xwidth){
					$width=$size[0];
				}else{
					$width=$xwidth;
				}
				$height = (int) @$xheight;
			}else{
				$width = (int) $xwidth;
				$height = (int) @$xheight;
			}
		}

	}

	//burada değişiklik yapsak performansda iyileşme olur. çöplük olmasına sebebiyet veriyor.
	$modtime = filemtime($src);
	if($newfilename==""){
		$_filename = md5($src.$modtime).'_'.$width.'.'.$ext;
	}else{
		$_filename = md5($src.$modtime).'_'.$newfilename.'.'.$ext;
	}

	if($wm==1){
	    
	    
	    
	}else{
	    
		if (file_exists(AP.'/'.$export_path.'/'.$_filename) && substr($_filename, 0, strpos($_filename, '_')) == md5($src.$modtime)) {
			
			//$idest = $icfunc ( 'cache/'.$_filename );
			if($noprint==""){
			    
			    echo root."/".$export_path."/".$_filename;
			    
			}else{
			    
			    return root."/".$export_path."/".$_filename;
			    
			}
			
		} else {

			$new_size = $width * 100 / $size[0];
			if ($height == 0 or $height==""){
				$height = $size[1] * $new_size / 100;
			}

			$x_ratio = $width / $size[0];
			$y_ratio = $height / $size[1];
			$ratio = min($x_ratio, $y_ratio);
			$use_x_ratio = ($x_ratio == $ratio);
			$new_width = ($use_x_ratio ? $width : floor($size[0] * $ratio));
			$new_height = (!$use_x_ratio ? $height : floor($size[1] * $ratio));

			$isrc = $icfunc($src);
			$idest = imagecreatetruecolor($new_width, $new_height);

			$new_left = 0;
			$new_top = 0;

			imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);

			imagejpeg($idest, AP.'/'.$export_path.'/'.$_filename, QUALITY);

			imagedestroy($isrc);
			imagedestroy($idest);
			
			if($noprint==""){
			    
			    echo root."/".$export_path."/".$_filename;
			    
			}else{
			    
			    return root."/".$export_path."/".$_filename;
			    
			}

		}

	}
	
}
Editor is loading...