Answers for "php image resize"

11

resize image html

<!--
Resize image using html 

When we add any image in 'src' attribute of 'img' tag then image appears with original size on webpage
and if we want to resize the image we have to add extra attribute i.e. height and width to img tag to resize the image
-->

Simple img tag
<img src="image_path" alt="Image Sample">

With height and width attribute
<img src="image_path" width="200" height="40" alt="Image Sample">

<!--
I hope it will help you.
Namaste
Stay Home Stay Safe
-->
Posted by: Guest on May-19-2020
0

php resize image

// resize on upload
$maxDim = 800;
$file_name = $_FILES['myFile']['tmp_name'];
list($width, $height, $type, $attr) = getimagesize( $file_name );
if ( $width > $maxDim || $height > $maxDim ) {
    $target_filename = $file_name;
    $ratio = $width/$height;
    if( $ratio > 1) {
        $new_width = $maxDim;
        $new_height = $maxDim/$ratio;
    } else {
        $new_width = $maxDim*$ratio;
        $new_height = $maxDim;
    }
    $src = imagecreatefromstring( file_get_contents( $file_name ) );
    $dst = imagecreatetruecolor( $new_width, $new_height );
    imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagedestroy( $src );
    imagepng( $dst, $target_filename ); // adjust format as needed
    imagedestroy( $dst );
}
Posted by: Guest on April-06-2021
0

php rotate image

After some INet searches and personal try-and-failures I succeed to rotate PNG images with preserving alpha channel transparency (semi transparency).

<?php
    $filename = 'YourFile.png';
    $rotang = 20; // Rotation angle
    $source = imagecreatefrompng($filename) or die('Error opening file '.$filename);
    imagealphablending($source, false);
    imagesavealpha($source, true);

    $rotation = imagerotate($source, $rotang, imageColorAllocateAlpha($source, 0, 0, 0, 127));
    imagealphablending($rotation, false);
    imagesavealpha($rotation, true);

    header('Content-type: image/png');
    imagepng($rotation);
    imagedestroy($source);
    imagedestroy($rotation);
?>
Posted by: Guest on May-02-2020
-1

php resize

Class resize
{
    // *** Class variables
    private $image;
    private $width;
    private $height;
 
    function __construct($fileName)
    {
        // *** Open up the file
        $this->image = $this->openImage($fileName);
 
        // *** Get width and height
        $this->width  = imagesx($this->image);
        $this->height = imagesy($this->image);
    }
}
Posted by: Guest on January-09-2021

Browse Popular Code Answers by Language