Jump to content

Recommended Posts

I have created an image resizing script, and am having trouble saving the resized image to the server. 

imagejpeg($image_resized, $imagelocation);

should save the image to where I designate $imagelocation as, but it doesn't.  Should i use fopen instead?  Also, is there any way i can make it so this script scan folders and  apply the script to certian files in them? And why does my script not work when I say open_image($picture); when picture is defined as $picture = 'flower.jpg'

 

Thanks, Dan

 

<?php
     //dan's lame image resizer
// Load image
$image = open_image('flower.jpg');
if ($image === false) { die ('Unable to open image'); }

// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

//echo 'Height: ' . imagesy($image) . ' pixels';
//echo 'Width: ' . imagesx($image) . ' pixels';

// Set a new width, and calculate new height
$new_width = 200;
$new_height = $height * ($new_width/$width);

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

// Display resized image
$imagelocation = 'resizedflower.jpg'
header('Content-Type: image/jpeg');
imagejpeg($image_resized, $imagelocation);
die();

function open_image ($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }

        return false;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/56226-picture-saving-after-resize/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.