Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Pages: [1] 2

Author Topic: [MOD] Automatic Resize Of Uploaded Images (sNews 1.4)  (Read 14307 times)

celebro

  • Guest
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« on: August 18, 2006, 08:52:04 PM »

[MOD] Auto-Resize Uploaded Images

PROBLEM:

When uploading images to your website with stock sNews 1.4 there is no
option to automatically re-size images so they do not break your layout.
I looked high and low for a php function that would automatically resize
the images to a specified width. Patric paved the way with a peice of code
which to him seemed broken. After carefull inspection that culprit was clear.
We were not specifyin the width we needed with that said here is the complete
working code. Which works on my site and as always

***********************************************************************
MAKE A BACKUP OF YOUR SNEWS.PHP FILE BEFORE MODIFYING ANYTHING I HOLD NO
RESPONSIBILITY IF THIS CODE DELETES YOUR DATA AND FILES WHICH IT WONT BUT
HEY YOU NEVER KNOW.
***********************************************************************  
SOLUTION:

First add the following to snews.php into the global settings:

Code: [Select]
$s['image_max_size'] = "375"; //specify resize width of uploaded imagesThen replace the default IMAGES function

Code: [Select]
//********
// IMAGES
//********
function images() {
if (isset($_GET['image'])) {
$file_to_delete = s('image_folder') ."/". $_GET['image'];
unlink($file_to_delete);
echo "<h2>". l('deleted_success') ."</h2><p><a href='" .s('website'). "images/'>". l('back') ."</a></p>";
} else { ?>
<h2><? echo l('images'); ?></h2>
<form name="imageformauthenticate" method="post" action="" enctype="multipart/form-data"></form>
<form name="imageform" method="post" action="" enctype="multipart/form-data">
<p>
<? echo l('upload_image'); ?>:</p>
<p><input type="file" name="imagefile" />
<input type="submit" name="upload" value="<? echo l('upload'); ?>" />
</p>
</form> <?
if(isset( $_POST['upload'] )) {
if ($_FILES['imagefile']['type']){
$upload_dir = s('image_folder') ."/";
copy ($_FILES['imagefile']['tmp_name'], $upload_dir .$_FILES['imagefile']['name']) or die ("Could not copy");
         echo "<div class=\"".s('css_success')."\"><h2>" .l('operation_completed'). "</h2></div>";
$kb_size = round(($_FILES['imagefile']['size'] / 1024), 1);
         echo "<p><b>".$_FILES['imagefile']['name']. "</b>  [ " .$kb_size. " KB ] [ " .$_FILES['imagefile']['type']." ]";
     } else {
            echo "<h2>" .l('admin_error'). "</h2>";
            echo "<p>" .l('image_error'). "</p>";
     }
} else {
$upload_dir = s('image_folder') ."/";
     $handle= opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
   if(!is_dir($file) && !is_link($file) && $file <> "Thumbs.db") {
    $filelist .= "<a href='$upload_dir$file'>".$file."</a> [ <a href='" .s('website'). "index.php?action=images&task=delete&image=" .$file. "'".if_javascript_on(" onclick='return confirm(\"".l('warning_delete')."\");'",'return').">Delete</a> ]
";
    }}
echo "<h2>". l('saved_images') .":</h2>";
echo "<p>" .$filelist. "</p>";
}}}
With the following:

Code: [Select]
// ********
// IMAGES
// ********

function images() {
    if (isset($_GET['image'])) {
        $file_to_delete = s('image_folder') ."/". $_GET['image'];
        unlink($file_to_delete);
        echo "<h2>". l('deleted_success') ."</h2><p><a href='" .s('website'). "images/'>". l('back') ."</a></p>";
    } else { ?>
    <h2><? echo l('images'); ?></h2>
    <form name="imageformauthenticate" method="post" action="" enctype="multipart/form-data"></form>
    <form name="imageform" method="post" action="" enctype="multipart/form-data">
        <p>
<? echo l('upload_image'); ?>:</p>
        <p><input type="file" name="imagefile" />
        <input type="submit" name="upload" value="<? echo l('upload'); ?>" />
</p>
    </form> <?
    if(isset( $_POST['upload'] )) {
        if ($_FILES['imagefile']['type']){
            //$upload_dir = s('image_folder') ."/";
            //copy ($_FILES['imagefile']['tmp_name'], $upload_dir .$_FILES['imagefile']['name']) or die ("Could not copy");
            //echo "<div class=\"".s('css_success')."\"><h2>" .l('operation_completed'). "</h2></div>";
                        //insert by Stretsh
            $upload_dir = s('image_folder') ."/";
            $known_photo_types = array(
                                'image/pjpeg' => 'jpg',
                                'image/jpeg' => 'jpg',
                                'image/gif' => 'gif',
                                'image/bmp' => 'bmp',
                                'image/x-png' => 'png'
                            );
            $gd_function_suffix = array(
                                'image/pjpeg' => 'JPEG',
                                'image/jpeg' => 'JPEG',
                                'image/gif' => 'GIF',
                                'image/bmp' => 'WBMP',
                                'image/x-png' => 'PNG'
                            );
            if(!array_key_exists($_FILES['imagefile']['type'], $known_photo_types))
            {
                echo "File ".$_FILES['imagefile']['name']." is not a photo
";
            }
            else
            {
                $new_id = $_FILES['imagefile']['name'];
                $filetype = $_FILES['imagefile']['type'];
                $extention = $known_photo_types[$filetype];
                $filename = $new_id.".".$extention;

                copy($_FILES['imagefile']['tmp_name'], $upload_dir."/".$filename) or die ("Could not copy");

                $size = GetImageSize( $upload_dir."/".$filename );
                if (($size[0] > s('image_max_size')) || ($size[1] > s('image_max_size'))) {
                    if($size[0] > $size[1])
                    {
                        $pic_width = s('image_max_size');
                        $pic_height = (int)(s('image_max_size') * $size[1] / $size[0]);
                    }
                    else
                    {
                        $pic_width = (int)(s('image_max_size') * $size[0] / $size[1]);
                        $pic_height = s('image_max_size');
                    }

                    $function_suffix = $gd_function_suffix[$filetype];
                    $function_to_read = "ImageCreateFrom".$function_suffix;
                    $function_to_write = "Image".$function_suffix;

                    $source_handle = $function_to_read ( $upload_dir."/".$filename );
                    if($source_handle)
                    {
                        $destination_handle = ImageCreateTrueColor ( $pic_width, $pic_height );
                        ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $pic_width, $pic_height, $size[0], $size[1] );
                    }
                    $function_to_write( $destination_handle, $upload_dir."/".$filename );
                    ImageDestroy($destination_handle );
                }
                echo "<h2>" .l('operation_completed'). "</h2>";
            }
            //end insert by Stretsh

            $kb_size = round(($_FILES['imagefile']['size'] / 1024), 1);
            echo "<p><b>".$_FILES['imagefile']['name']. "</b>  [ " .$kb_size. " KB ] [ " .$_FILES['imagefile']['type']." ]";
        } else {
            echo "<h2>" .l('admin_error'). "</h2>";
            echo "<p>" .l('image_error'). "</p>";
        }
    } else {
        $upload_dir = s('image_folder') ."/";
        $handle= opendir($upload_dir);
        $filelist = "";
        while ($file = readdir($handle)) {
           if(!is_dir($file) && !is_link($file) && $file <> "Thumbs.db") {
            $filelist .= "<a href='$upload_dir$file'>".$file."</a> [ <a href='" .s('website'). "index.php?action=images&task=delete&image=" .$file. "'".if_javascript_on(" onclick='return confirm(\"".l('warning_delete')."\");'",'return').">Delete</a> ]
";
    }}
    echo "<h2>". l('saved_images') .":</h2>";
    echo "<p>" .$filelist. "</p>";
}}}
After uploading your images they should now be resized without loosing resolution to the specified width. I wish it would be able to open a popup window with the original size but maybe I am asking too much :)


*** All credits go to the original Author Stretsh I have not changed or modified anything just debugged to find out why it would not work.
« Last Edit: June 13, 2008, 03:41:17 PM by Keyrocks »
Logged

Keyrocks

  • Doug
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 451
  • Posts: 6228
  • Semantically Challenged
    • snews.ca
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #1 on: August 18, 2006, 09:52:07 PM »

This looks like a useful utility celebro. Your PHP knowledge will be a welcome addition here at sNews Forums... another collaboration meets with success! Being able to set the maximum width within the site's global variables is particularly convenient, allowing for a one-point change to match either narrower or wider templates.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4957
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #2 on: August 19, 2006, 12:22:36 PM »

The UNKNOWN author is STRETSH.

And credits to you to Celebro for your debugging of my not so complete codepost, hehe...Sorry bout that :)

PAtric.
Logged
My sNews site
"It's only dead fish that goes with the flow... "
Updated, online again - Free designs

4Sandrix

  • Hero Member
  • *****
  • Karma: -13
  • Posts: 613
    • http://fedrick.deviantart.com
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #3 on: August 19, 2006, 12:59:17 PM »

This is great thanks... Now I don't need to resize them by my self
Logged
I'm kind of like Han Solo always strokin' my own Wookiee
I'm the root of all that's evil yeah but you can call me Cookie

Tom Gebauer

  • Newbie
  • *
  • Karma: 0
  • Posts: 11
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #4 on: August 19, 2006, 04:08:35 PM »

Sticky?
Logged

Mika

  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 9
  • Posts: 1408
    • http://www.ni5ni6.com/
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #5 on: August 19, 2006, 04:25:01 PM »

why? we still have search feature on this forum :)
Logged
http://www.ni5ni6.com/ - Tutorials, Mods and How-To's about sNews CMS
sNews 1.6 Developers Edition - commented sNews 1.6 version

Keyrocks

  • Doug
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 451
  • Posts: 6228
  • Semantically Challenged
    • snews.ca
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #6 on: August 19, 2006, 04:31:49 PM »

Quote from: Tommy-G
Sticky?
Sticky what?
Quote from: 4Sandrix
This is great thanks... Now I don't need to resize them by my self
Notwithstanding... of course... that it is still important to reduce the size and resolution of your images AND reduce BEFORE uploading them to your site... to reduce bandwidth. If you upload an image in its original size/resolution, and it is about 5 MB in size in that form, the image will still be a "heavy" file even after this utility resizes it for display... the resizing does not change the original stored image's resolution and overall byte size. So, if a file is still huge, it consumes far more bandwidth each and every time it is served in a page-view to site viewers.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

celebro

  • Guest
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #7 on: August 19, 2006, 04:59:39 PM »

Quote from: keyrocks
Quote from: Tommy-G
Sticky?
Sticky what?
Quote from: 4Sandrix
This is great thanks... Now I don't need to resize them by my self
Notwithstanding... of course... that it is still important to reduce the size and resolution of your images AND reduce BEFORE uploading them to your site... to reduce bandwidth. If you upload an image in its original size/resolution, and it is about 5 MB in size in that form, the image will still be a "heavy" file even after this utility resizes it for display... the resizing does not change the original stored image's resolution and overall byte size. So, if a file is still huge, it consumes far more bandwidth each and every time it is served in a page-view to site viewers.
It does change the size, as it deletes the original and replaces it with the smaller sized image (785kb was reduced to 109kb image). But you are correct in the bandwidth issue uploading 5Mb is still 5Mb,.. but the overcomming factor is after its being served.

My main need for reducing the images was for clients who upload images directly of there cameras which we all know tends to be 2000+ pixels images on the average.

On a side note: Currently working on an Image Gallery should be done in a few days. I dont code anything personally besides the factors of integrating I dont like to re-invent the wheel.
Logged

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4957
  • “I'm a self-made man and worships my creator.”
    • p-ahlqvist.com
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #8 on: August 19, 2006, 05:41:35 PM »

Well, this here solution have been on board for some time before we changed forum software. It has disappeared somewhere along the line, but this is a good solution. I use it on my site, and I have integrated it onto several others... All in the name of bandwidth ;). Specially when uploading directly from camera, and not knowing anything abaout anything really... It's plain good ol' service towards the customer that might not know anything about this, and just fire up those darn big photos from camera directly to the site, which makes it fairly... freaking slow loading after a while...

Patric

PS. don't know why I posted that. But you all know me, hehe... disregard my posts if you donät like'em or understand them, whuhaaaaa ;)
Logged
My sNews site
"It's only dead fish that goes with the flow... "
Updated, online again - Free designs

Keyrocks

  • Doug
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 451
  • Posts: 6228
  • Semantically Challenged
    • snews.ca
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #9 on: August 19, 2006, 10:27:14 PM »

Quote from: Patric
PS. don't know why I posted that. But you all know me, hehe... disregard my posts if you donät like'em or understand them, whuhaaaaa ;)
I think sharing the reasoning behind why we do some of the things we do does have value... at the very least... other ways of 'skinning the cat' and better understanding other points of view... or finding out when we're really off-base on an idea (saves time wasted going down a one-way path, eh?) :)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Tom Gebauer

  • Newbie
  • *
  • Karma: 0
  • Posts: 11
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #10 on: August 22, 2006, 05:46:07 AM »

Sorry, sometimes I get a bit hopped up on the "sticky".
Logged

mejobloggs

  • Full Member
  • ***
  • Karma: 0
  • Posts: 127
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #11 on: August 24, 2006, 01:26:41 AM »

Sweet, that means I don't have to finish my one. Far too busy :(

Cheers!
Logged

oz_sprog

  • Newbie
  • *
  • Karma: 0
  • Posts: 15
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #12 on: August 24, 2006, 02:33:31 AM »

if some one can find a way to include these images into the rss feed, I'll be in debt to them for life!! I've spent most of the day trying to figure out a way to do it, I've not got much hair left now.
Logged

Magic

  • Newbie
  • *
  • Karma: 0
  • Posts: 7
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #13 on: August 29, 2006, 08:44:54 PM »

Hello all.

Another option here is to use a style tag to float the image left or right in your content and then clear the float.

- - - - - - - - - - - -

To begin, within CENTER, add style to the image here...

      if ($r['image'] <> "") { ?>
         <img src="<? echo s('website') .s('image_folder'); ?>/<? echo $r['image']; ?>" alt="<? echo $r['title']; ?>" title="<? echo $r['title']; ?>" style="float:right;margin-top:0px;margin-bottom:20px;margin-left:20px;clear:right" />


Next, I have wrapped each article within CENTER in an empty set of <div></div> tags, beginning here...

         if ($category == "0") { $category = s('home'); }
         if ($r['displaytitle'] == "YES" AND $article == "") {       
         echo "
         <div>


and then ending that open <div> tag and clearing the open float here...


               if ($commentable == "YES" or $commentable == "FREEZ") {
                  echo "<img src='" .s('website'). "images/comment.gif' alt='' /> <a href='" .s('website'). $category. "/" .$r['seftitle']. "/'>". l('comments') ."(". $comments_num .")</a> ";
                     }
               echo "<img src='" .s('website'). "images/timeicon.gif' alt='' /> " .$fp_date_format. "</p>


         </div>
      <div style='clear:right'></div>

- - - - - - - - - -

You will also need to clear the float at the beginning of the commentsbox within the COMMENTS section.

   if ($freeze_status <> "freezed") { ?>

   <div id="clear" style="clear:right"></div>

   <div class="commentsbox">
      <h2><? echo l('addcomment') ?></h2>   
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

- - - - - - - - - -

This will allow you to include an image of any reasonable size within the content of any article.

http://www [dot] webpaint [dot] net/news/

I don't want the bots in there just yet, so paste this into your browser to see my work in progress.

All comments are welcome.

[Magic]
« Last Edit: June 13, 2008, 03:42:04 PM by Keyrocks »
Logged
OK dummy . . . get off the phone.

celebro

  • Guest
[MOD] Automatic Resize Of Uploaded Images (sNews 1.4)
« Reply #14 on: August 30, 2006, 01:52:38 AM »

Hello Magic,
    Your solutios open the way for another mod/addon however it defeats the purpose of this contribution which is the actual resize of an image while maintaining the aspect ratio and quality of the image. I came up with an idea from your response here, I am currently working on some other stuff but when I have the time I will try and implent it with this contribution (hint: css popup)

Also your contribution could use a bit of cleaning up, by removing the "hardcoded" in-line style css definitions, and giving them a class so editing could be done via the CSS file itself.
Logged
Pages: [1] 2