PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to save an image to directory after resize in php?

 October 10, 2022     gd, php     No comments   

Issue

I am using the GD libary in php to resize uploaded images which works fine but I just can't figure out how to save the resized image to a directory. How do I go about this? I don't seem to get any errors.

   <?php
    // The file
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    $filename = 'test.jpg';
    $percent = 0.5;


    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
            {

    $filename = $_FILES['photoimg']['tmp_name'];

    // Content type
    header('Content-Type: image/jpeg');

    // Get new dimensions
    list($width, $height) = getimagesize($filename);
    $new_width = $width * $percent;
    $new_height = $height * $percent;

    // Resample
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    // Output
    imagejpeg($image_p, null, 100);

    //This is my attempt to save the image that does not work
 if (move_uploaded_file($image, "memberFiles/saved_image.jpg")) {
}
    }
    ?>

Solution

Just use:

imagejpeg($image_p, 'some/other/existing/directory/result.jpg');


Answered By - Mark Setchell
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing