Jump to content

how to resize an image and save as new file with gd? (NOT SOLVED)


DaveLinger

Recommended Posts

Hello everyone,

I'm very familiar with PHP but NOT very familiar with the GD image functions.

I'd like to upload an image, have it resized to, say, 150x150 (not exactly that, I'd like it to make it 150 by whatever the other dimension needs to be for it to be proportional), then saved as imagename_thumbnail.whatever . I can handle the file upload and pass the file url to the next php page, but where do I go from there?

Thanks!
VERY helpful. Thanks!

[code]<?php
function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file) ;
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight) ;
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file) ;
        break;
        case 'gif':
                    $image = imagecreatefromgif($file) ;
        break;
        case 'png':
                    $image = imagecreatefrompng($file) ;
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    imagejpeg($tn, $save, 100) ;
    return;
}

#####################
#
#    Example usage
#
#####################

$save = 'myfile.jpg';
$file = 'original.jpg';
$t_w = 120;
$t_h = 120;
$o_path = " ";
$s_path = " ";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);
?> [/code]
err...

[code]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload" />
</form>

<?php

// Here's Where I define the Resize_Image function //

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file) ;
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight) ;
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file) ;
        break;
        case 'gif':
                    $image = imagecreatefromgif($file) ;
        break;
        case 'png':
                    $image = imagecreatefrompng($file) ;
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    imagejpeg($tn, $save, 100) ;
    return;
}

// Here's where it checks to see if it's uploaded, and starts an if statement //

if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
copy($_FILES["userfile"]["tmp_name"], "../files/" . $_FILES["userfile"]["name"]);

// Since the image was uploaded, let's create that thumbnail //

$save = 'myfile.jpg';
$file = "$_FILES['userfile']['name']";
$t_w = 120;
$t_h = 120;
$o_path = "files";
$s_path = "files/thumbnails";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

// otherwise, just err //

}else{

echo "upload error";
}
?>[/code]

"Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/s/b/o/sboyce1/html/photos/files/upload.php on line 52"

it gives me that on page load... doesnt even show the form...
got it.

[code]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload" />
</form>

<?php
if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
copy($_FILES["userfile"]["tmp_name"], "files/" . $_FILES["userfile"]["name"]);

$uploadedimage = $_FILES['userfile']['name'];

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file) ;
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight) ;
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file) ;
        break;
        case 'gif':
                    $image = imagecreatefromgif($file) ;
        break;
        case 'png':
                    $image = imagecreatefrompng($file) ;
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    imagejpeg($tn, $save, 100) ;
    return;
}

$save = 'myfile.jpg';
$file = $uploadedimage;
$t_w = 120;
$t_h = 120;
$o_path = "files/";
$s_path = "files/thumbnails/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

echo "w00t.";

}
?>[/code]
hokay. Also trying to apply text as a watermark before the resizing, so it appears on both images at proportional size.

Not working:

[code]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2048000">
File: <input name="userfile" type="file" /><br />
<input type="submit" value="Upload" />
</form>

<?php
if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
copy($_FILES["userfile"]["tmp_name"], "files/" . $_FILES["userfile"]["name"]);

$uploadedimage = $_FILES['userfile']['name'];

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file) ;
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight) ;
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file) ;
        break;
        case 'gif':
                    $image = imagecreatefromgif($file) ;
        break;
        case 'png':
                    $image = imagecreatefrompng($file) ;
        break;
    }
   
// watermarking //
$white = imagecolorallocate($image, 255, 255, 255);
$text = 'MyText';
$font = 'CALIBRI.TTF';
imagettftext($image, 10, 0, 10, 10, $white, $font, $text);
imagejpeg($image, $save, 100);
// done watermarking //

    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
    imagejpeg($tn, $save, 100) ;
    return;
}

$save = $uploadedimage;
$file = $uploadedimage;
$t_w = 120;
$t_h = 120;
$o_path = "files/";
$s_path = "files/thumbnails/";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

echo "w00t.<br><br><img src=\"files/";
echo $uploadedimage;
echo "\"><br><br><img src=\"files/thumbnails/";
echo $uploadedimage;
echo "\">";

}
?>[/code]
Don't know if this is any help, but found a script which can be used to add text the the center of an image (i.e. watermark). Perhaps you could get some ideas from there how to get your code working. Im quite new to GD stuff myself so. The link is:
[url=http://www.nuonce.net/code-library/1084673172.html]http://www.nuonce.net/code-library/1084673172.html[/url]
Sequence would be something like this
[code]
move_uploaded_file($_FILE['uploadedfile']['tmp_name'], 'mybigpic.jpg');

$big = imagecreatetruecolor ('mybigpic.jpg');

  // watermark code
  // now save
imagejpeg($big, 'mybigpic.jpg', 100);

$small = imagecreatetruecolor (150, 150);
    // resize code from big to small
    // save small image
imagejpeg($small, 'mysmallpic.jpg', 75);
imagedestroy($big);
imagedestroy($small);[/code]

I have a class which you specify a maximum width, say 150px.  This class then create a thumbnail images of the original image.  Only that if width > height, then width will be 150 and height will be whatever proportional.  if height > width, then height will be 150 and width will be.. u know.  Thumb created is proportional, not stretched nor truncated.

If you need this class, i'll post if up or send to you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.