Jump to content


Photo

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


  • Please log in to reply
18 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 05 July 2006 - 03:06 AM

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!
http://www.DaveLinger.com
dave at linger dot com

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 05 July 2006 - 03:35 AM

This might be of some help...

http://www.nstoia.co...esize/index.php

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 05 July 2006 - 04:03 AM

VERY helpful. Thanks!

<?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);
?>

http://www.DaveLinger.com
dave at linger dot com

#4 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 05 July 2006 - 04:17 AM

err...

<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";
}
?>

"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...
http://www.DaveLinger.com
dave at linger dot com

#5 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 05 July 2006 - 04:35 AM

got it.

<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.";

}
?>

http://www.DaveLinger.com
dave at linger dot com

#6 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 05 July 2006 - 12:24 PM

hokay. Also trying to apply text as a watermark before the resizing, so it appears on both images at proportional size.

Not working:

<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 "\">";

}
?>

http://www.DaveLinger.com
dave at linger dot com

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 05 July 2006 - 07:00 PM

bump
http://www.DaveLinger.com
dave at linger dot com

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 July 2006 - 07:02 PM

Stop bumping topics that are only a few hours old. Please wait for at least 4 - 12 hours before bumping a topic again. I have removed your  last two replies.

#9 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 06 July 2006 - 02:54 AM

bump
http://www.DaveLinger.com
dave at linger dot com

#10 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 06 July 2006 - 11:54 AM

bump
http://www.DaveLinger.com
dave at linger dot com

#11 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 06 July 2006 - 12:58 PM

Sorry been in meetings all week.

Let me work on this tonight and see if I can scratch together a solution for you.

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#12 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 06 July 2006 - 11:11 PM

bump
http://www.DaveLinger.com
dave at linger dot com

#13 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 07 July 2006 - 12:09 PM

bump :-[
http://www.DaveLinger.com
dave at linger dot com

#14 kopander

kopander
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationFinland

Posted 07 July 2006 - 12:47 PM

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:
http://www.nuonce.ne...1084673172.html

#15 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 08 July 2006 - 02:15 AM

well I already know how to watermark it, I just dont know how to get one script to upload, save, watermark, resize, then save as. =/
http://www.DaveLinger.com
dave at linger dot com

#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 08 July 2006 - 09:39 AM

Sequence would be something like this
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);


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 08 July 2006 - 11:41 AM

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.
Life's too short for arguing.

#18 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 10 July 2006 - 01:49 PM

bump
http://www.DaveLinger.com
dave at linger dot com

#19 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 10 July 2006 - 06:51 PM

You say you know how to add watermarks.
You have sample resize code posted earlier in the thread
I've shown you how to put it together.

What do you want now?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users