Modification to create image download link Modification to create image download link
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

Modification to create image download link

Started by adrianbj, July 05, 2005, 02:26:28 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

adrianbj

First of all, I tried to add this post to modifications, but I didn't have rights to post a new topic, so here goes. Something that has always bugged me a little about Coppermine was that clicking on the normal size image spawns a new window containing the fullsize version. I think it would be better (especially for non-tech users) that there is a download link to the full version. Sometimes these images can be many MB in size and a user may not realize this until their browser starts to download it. I have made a modification myself to address this and thought I'd share it with anyone else who might be interested.

I used to use version 1.3 and so the code to be modified was in the displayimage.php file, but I have just installed 1.41 and it now appears that you have to make this mod to the theme.php file.
Here are my changes:

// The original content of theme.php (note this is only present in the sample theme)

    if ($mime_content['content']=='image') {
        if (isset($image_size['reduced'])) {
            $winsizeX = $CURRENT_PIC_DATA['pwidth']+5;  //the +'s are the mysterious FF and IE paddings
            $winsizeY = $CURRENT_PIC_DATA['pheight']+3; //the +'s are the mysterious FF and IE paddings
            $pic_html = "<a href=\"javascript:;\" onclick=\"MM_openBrWindow('displayimage.php?pid=$pid&amp;fullsize=1','" . uniqid(rand()) . "','scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=$winsizeX,height=$winsizeY')\">";
            $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
            $pic_html .= "<img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /><br />";
            $pic_html .= "</a>\n";


// My modification below creates a download link showing the size of the image and prevents opening of fullsize image in new window

    if ($mime_content['content']=='image') {
    if (isset($image_size['reduced'])) {
        $filesize = $CURRENT_PIC_DATA['filesize'];
$filesizecorr = intval($filesize/1024);
$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
        $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
$picturefs_url = str_replace("normal_", "", $picture_url);
        $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
        $pic_html .= "<center><img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /></center><br />";
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
    $pic_html .= "</a>\n";


You will also need to create a new file called download.php in the root of your coppermine installation with these contents

<?php
     
if ($_REQUEST ['getfile']){
        
$file $_REQUEST ['getfile'];
     
     }

$save_as_name basename($file);   
ini_set('session.cache_limiter''');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header("Content-Type: application/octet-stream");
header("Content-Disposition: disposition-type=attachment; filename=\"$save_as_name\"");

readfile($file);

?>


Anyway, hope that is useful to someone.

Adrian



HanOverFist

everything seems to work but when I click the link I'm getting strange DL problems.
file sizes are over 1.5 meg do not fully DL
The DL dialog box finishes transfering the file but when trying to open the file I get a corrupt or truncated file error message.
Server setting?
CPG 1.3.3

HanOverFist

I would love to get a reply here.
cpg133 with this mod truncates every file over 1.9 megs at 2,000,000 bytes.
cpg141 with this mod truncates every file over 1.9 megs at 2,000,000 bytes.
Everytime.
Is this a php server setting that is doing this.
It would be great to get this resolved.
Is there any more info you need from me for a response?

Jay

Nibbler


HanOverFist

thanks nibbler

That is what I was looking for.
now where do I insert this bit of code?

<?php
function readfile_chunked($filename,$retbytes=true) {
  $chunksize = 1*(1024*1024); // how many bytes per chunk
  $buffer = '';
  $cnt =0;
  // $handle = fopen($filename, 'rb');
  $handle = fopen($filename, 'rb');
  if ($handle === false) {
      return false;
  }
  while (!feof($handle)) {
      $buffer = fread($handle, $chunksize);
      echo $buffer;
       ob_flush();
      flush();
      if ($retbytes) {
          $cnt += strlen($buffer);
      }
  }
      $status = fclose($handle);
  if ($retbytes && $status) {
      return $cnt; // return num. bytes delivered like readfile() does.
  }
  return $status;

}
?>



In the new download.php file that I created on line 15? (replacing the instance of readfile?)
or do I make a new php file out of it called readfile.php?

tia

Jay

Nibbler

You place the function definition somewhere in the file before the call to read($file), and replace the call to read($file) with readfile_chunked($file);

HanOverFist


serhathakan

i am using 1.3.2 and the original code is different and

when i change it to

if ($mime_content['content']=='image') {
    if (isset($image_size['reduced'])) {
        $filesize = $CURRENT_PIC_DATA['filesize'];
$filesizecorr = intval($filesize/1024);
$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
        $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
$picturefs_url = str_replace("normal_", "", $picture_url);
        $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
        $pic_html .= "<center><img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /></center><br />";
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
    $pic_html .= "</a>\n";




it giving error

and my original code is



if ($mime_content['content']=='image') {
        if (isset($image_size['reduced'])) {
            $winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
            $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
            $pic_html = "<a href=\"javascript:;\" onClick=\"MM_openBrWindow('displayimage.php?pid=$pid&fullsize=1','" . uniqid(rand()) . "','scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=$winsizeX,height=$winsizeY')\">";
            $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
            $pic_html .= "<img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /><br />";
            $pic_html .= "</a>\n";

Nibbler

Are we allowed to know the error that it gives you ?

serhathakan

sure here is the code

Parse error: parse error, unexpected $ in /home/tasarim/public_html/galeri/displayimage.php on line 646

Loki66

Is there a way to protect the image using this download ? Is it possible to put images in protected directories ?

Thanks,

L

serhathakan


adrianbj

Well thanks everyone for improving on my mod. I didn't have any download problems on my server with my php settings so I didn't realize the need for the chunkfile way of doing things. I have my the changes to my setup also.

Cheers,

A.

BT-loader

#13
I´ve just installed this mod/hack but it seems that there is an error when saving the image to my hard drive.  :\'(
The image is saved as "document" without any extension, why is that?

(I´m using 1.3.3)

EDIT:  Nevermind, i finally got it to work.

Thanks for a great mod.  ;D
:: BT-loader ::
"The one and only..."

Joachim Müller

Quote from: BT-loader on August 25, 2005, 10:01:06 PM
EDIT:  Nevermind, i finally got it to work.
Then why don't you tell others who might have the same issues you were having? Be a giver instead of always being a taker.

BT-loader

Quote from: GauGauThen why don't you tell others who might have the same issues you were having? Be a giver instead of always being a taker.
It was a stupid thing really, i just forgot to copy all the code.  :-[

Quote from: adrianbj// My modification below creates a download link showing the size of the image and prevents opening of fullsize image in new window

    if ($mime_content['content']=='image') {
    if (isset($image_size['reduced'])) {
        $filesize = $CURRENT_PIC_DATA['filesize'];
$filesizecorr = intval($filesize/1024);
$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
        $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
$picturefs_url = str_replace("normal_", "", $picture_url);
        $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
        $pic_html .= "<center><img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /></center><br />";
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
    $pic_html .= "</a>\n";
Quote
:: BT-loader ::
"The one and only..."

mylogon

#16
Any thought on how to modify the code for pictures that are resized and therefore will not cause either the "Normal" view or create the download link because they do not trigger that section?

Anyone have any ideas?

shniz123


outpatient

#18
Hey kids,

I'm looking for a mod to stop my videos from playing automatically and to offer a download link as an option.  I know there was one that worked just for the videos, because I've used it before, only I can't for the life of me, find it.  ::)

Would this mod make all images available for download? Can someone point me in the right direction?

I'd really appreciate it, thanks


eta: nevermind, I found it!  ;) here

EtaBeta

Hi!
Nice mod!
Is it possible to allow ONLY registerd users to download? How??? I am very iterested to this modification because iI don' want all the visitors be able to download pictures from my gallery.
Thanks to all