Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Excerpt

The code cfolderResizeImage() will enable you to resize the source image with keeping aspect ratio in PHP.


Code Block
languagephp
function cfolderResizeImage($file, fn$target_resize($imagewith_resourceno_id,$width,$height)
{
    ext, $target_width = 40;
   , $target_height = 40;)
{
    $h$source_properties = $height * $target_width / $widthgetimagesize($file);
    if ($h>$target_height)
    {
        $w = $target_width$image_type = $source_properties[2];
    }
    elseif( $image_type == IMAGETYPE_JPEG )
    {
  
      $h = $target_height;
        $w = $width * $target_height / $height;$image_resource_id = imagecreatefromjpeg($file); 
    }

    $target_layer=imagecreatetruecolor($w,$h);
    imagecopyresampled($target_layer, = fn_resize($image_resource_id,$source_properties[0,0,0,0,$w,$h, $width,$height],$source_properties[1], $target_width, $target_height);
      return  imagejpeg($target_layer;
}

function cfolderResizeImage($file, $target_with_no_ext)
{
    $source_properties = getimagesize($file. ".jpg", $target_width, $target_height);
    $image_type = $source_properties[2]; }
    ifelseif( $image_type == IMAGETYPE_JPEGGIF )
    {   
        $image_resource_id = imagecreatefromjpegimagecreatefromgif($file);  
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1], $target_width, $target_height);
        imagejpegimagegif($target_layer, $target_with_no_ext. ".jpg"gif", $target_width, $target_height);
    }
    elseif( $image_type == IMAGETYPE_GIFPNG )
    {  
        $image_resource_id = imagecreatefromgifimagecreatefrompng($file);
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1], $target_width, $target_height);
        imagegifimagepng($target_layer, $target_with_no_ext. ".gif"png", $target_width, $target_height);
    }
}
 
function fn_resize($image_resource_id,  elseif( $image_type == IMAGETYPE_PNG $width, $height, $target_width, $target_height)
{
    $h = $height * $target_width / $width;
    if ($h>$target_height)
    {
        $image_resource_id$w = imagecreatefrompng($file); $target_width;
    }
    else
    {
      $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);
        imagepng($target_layer, $target_with_no_ext. ".png");
    }
}  $h = $target_height;
        $w = $width * $target_height / $height;
    }
 
    $target_layer=imagecreatetruecolor($w,$h);
    imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$w,$h, $width,$height);
    return $target_layer;
}