"16x16", "icon" => "32x32", //"thumb" => "50x50", "small" => "100x100", "compact" => "160x160", "medium" => "240x240", "large" => "480x480", "grande" => "600x600" ); if(isset( $sizes[$size] ) ) $size = $sizes[$size]; $dim = explode("x", $size); if(!$dim[0]) $dim[0] = "0"; if(!$dim[1]) $dim[1] = "0"; if( count($dim) != 2 or !is_numeric($dim[0]) or !is_numeric($dim[1]) or $dim[0]>2048 or $dim[1]>2048 ){ error404(); } $width = $dim[0]; $height = $dim[1]; extract(pathinfo($from)); $path = "$dirname/$width"."x".$height; $result = make_directories($path); if(!$result and $devmode){ die("Error Folder"); } $to = "$path/$filename.$extension"; // die($to); // include('class.phpthumb.php'); // $info = PhpThumbFactory::determineFormat ($from); if(!file_exists($to)){ // //create_thumb($from, $to, $width, $height, 90); list($w, $h, $type, $attr) = getimagesize($from); if (!$w || !$h) { if($devmode) die('Error: Invalid image dimensions'); error404(); } // Calculate height if only width is given if ($height == 0 && $width > 0) { $height = round(($h / $w) * $width); } // Calculate width if only height is given if ($width == 0 && $height > 0) { $width = round(($w / $h) * $height); } // روی تصویرِ خیلی پهن، round() بُعدِ دوم را صفر می‌کند (مثلاً 3292x403 با s=3x → ارتفاع ۰) // و از آن‌جا Division by zero و imagecreatetruecolor نامعتبر درمی‌آید. حداقل یک پیکسل. if ($width < 1) { $width = 1; } if ($height < 1) { $height = 1; } try { // Check if source file exists if (!file_exists($from)) { throw new Exception("Error: Source file does not exist: $from"); } // Load the image $image = new \Gumlet\ImageResize($from); $image->quality_jpg = 100; if ($w != $width || $h != $height) { // Check if aspect ratios match $originalAspectRatio = $w / $h; $newAspectRatio = $width / $height; if ($originalAspectRatio == $newAspectRatio) { // Aspect ratios match, resize normally $image->resize($width, $height, true); } else { // if ($width < $w) { // Resize and crop bottom if new height is less than original height $image->resizeToWidth($width); if($devmode){ $image->save($to); // die($to); }else{ $image->crop( $width, $height,false, 1);//CROPTOP } // $image->crop(0, 0, $width, $height); } else { // Maintain aspect ratio without stretching or cropping $image->resize($width, $height, true); } } $image->save($to); } else { if ($from != $to) { copy($from, $to); }else{ echo "."; } } // Check if destination file exists before optimization if (!file_exists($to)) { //echo ""; throw new Exception("Error: Destination file does not exist: $to"); } if($optimize_mode and file_exists($to) ){ $optimizerChain = \Spatie\ImageOptimizer\OptimizerChainFactory::create(); $optimizerChain->optimize($to); } chmod($to, 0777); } catch (Exception $e) { // die($e); if($devmode) die($e->getMessage()); error_log("image.php: ".$e->getMessage()); error404(); } // list($w, $h, $type, $attr) = getimagesize($from); // if(!$w or !$h){ // error404(); // } // if($height == 0 and $width>0) // { // $height = round(($h / $w) * $width); // } // if($width == 0 and $height>0) // { // $width = round(($w / $h) * $height); // } // try // { // if($w != $width or $h != $height) // { // $image = new \Gumlet\ImageResize($from); // $image->quality_jpg = 99; // $image->resizeToWidth($width); // $image->crop( $width, $height,false, CROPTOP); // // $image->resize($width, $height, true); // $image->save($to); // }else{ // if($from != $to){ // copy($from ,$to); // } // } // $optimizerChain = \Spatie\ImageOptimizer\OptimizerChainFactory::create(); // $optimizerChain->optimize($to, $to); // // die($to); // chmod($to,0777); // // chown($to, "root"); // // chgrp($to, "root"); // } // catch (Exception $e) // { // die($e); // } } switch( $extension ) { case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpeg"; break; case "webp": $ctype="image/webp"; break; default: } header('Content-type: ' . $ctype); readfile($to); die(); function make_directories($path) { if(@mkdir($path, 0777) or file_exists($path)) return true; return (make_directories(dirname($path)) and mkdir($path, 0777)); } function error404() { // هدرهای کشِ بالای فایل نباید ۴۰۴ را ۲۴ ساعت روی مرورگر/CDN نگه دارند header("Cache-Control: no-store, max-age=0"); header_remove("Expires"); header("HTTP/1.0 404 Not Found"); die(); }