Search code examples
phpimageimage-resizing

Save path for resized img PHP


Here's my issue, i did this :

        $fichierIMG = $_FILES['image']['tmp_name'];

        $tailleSource = getimagesize($fichierIMG);

        $image_type = $tailleSource[2]; 

        if( $image_type == IMAGETYPE_JPEG ) {   
        $imageSource = imagecreatefromjpeg($fichierIMG);  
        $imgResize = redimension($imageSource,$tailleSource[0],$tailleSource[1]);
        imagejpeg($imgResize,$_FILES['image']['name'] . "_thump.jpg");
        }

        elseif( $image_type == IMAGETYPE_PNG ) {
        $imageSource = imagecreatefrompng($fichierIMG); 
        $imgResize = redimension($imageSource,$tailleSource[0],$tailleSource[1]);
        imagepng($imgResize,$_FILES['image']['name'] . "_thump.png");
        }

        function redimension($imageSource,$width,$height) {
            $img_width =200;
            $img_height =200;
            $imgResize=imagecreatetruecolor($img_width,$img_height);
            imagecopyresampled($imgResize,$imageSource,0,0,0,0,$img_width,$img_height, $width,$height);
            return $imgResize;
            }

But my file with "_thump.xxx" doesn't appear in my uploads folder, what am I doing wrong?


Solution

  • You're making all your changes based on a temporary file.

    also, you never specify an uploads directory anywhere:

    imagejpeg($imgResize,$_FILES['image']['name'] . "_thump.jpg"); should probably be imagejpeg($imgResize,"uploads/".$_FILES['image']['name'] . "_thump.jpg");

    Wouldn't hurt to use move_uploaded_file to ensure that the file is actually uploaded correctly too.

    https://www.php.net/manual/en/function.move-uploaded-file.php