Welcome Guest, Not a member yet? Register   Sign In
Getting uploadify to work
#51

[eluser]Puspex[/eluser]
Hey everybody!

I did some modifications to Bob Sawyer's uploadify.php script, posted on http://www.reloadedpc.com/code-igniter/j...deigniter/
What i did is if u wish to upload images (jpg,gif,png) it uploads the images, resizes and crops them to your needs, and returns their names to the uploadify.php view.

Here is the complete code located in the base_url(). "js/uploadify/uploadify.php" .

I added:
Code:
function uploadImage($inputName, $uploadDir, $imageWidth = 500, $thumbWidth = 100, $thumbHeight = 100)
    {
        $image     = $_FILES[$inputName];
        $imagePath = '';
        $thumbnailPath = '';
        // if a file is given
        if (trim($image['tmp_name']) != '') {
            $ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
            // generate a random new file name to avoid name conflict
            //$imagePath = $image['name'];
            $mainImageName = md5(rand() * time());
            $imagePath = $mainImageName . ".$ext";
            list($width, $height, $type, $attr) = getimagesize($image['tmp_name']);
            // make sure the image width does not exceed the
            // maximum allowed width
            
            if (true && $width > $imageWidth) {
                $result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, $imageWidth);
                $imagePath = $result;
            } else {
                $result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
            }    
            
            if ($result) {
                // create thumbnail
                //$thumbnailPath = $image['tmp_name'];
                //$thumbnailPath =  md5(rand() * time()) . ".$ext";
                $thumbnailPath =  $mainImageName . "_thumb" . ".$ext";
                $result = cropImage($thumbWidth, $thumbHeight, $uploadDir . $imagePath, $ext, $uploadDir . $thumbnailPath);
                //$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
                // create thumbnail failed, delete the image
    
                if (!$result) {
                    unlink($uploadDir . $imagePath);
                    $imagePath = $thumbnailPath = '';
                } else {
                    $thumbnailPath = $result;
                }    
            } else {
                // the product cannot be upload / resized
                $imagePath = $thumbnailPath = '';
            }
        }
        return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
    }
    
    function createThumbnail($srcFile, $destFile, $width, $quality = 100)
    {
        $thumbnail = '';
        if (file_exists($srcFile)  && isset($destFile))
        {
            $size        = getimagesize($srcFile);
            $w           = number_format($width, 0, ',', '');
            $h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
    
            $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
        }
        // return the thumbnail file name on sucess or blank on fail
        return basename($thumbnail);
    }
    
    /*
        Copy an image to a destination file. The destination
        image size will be $w X $h pixels
    */
    
    function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
    {
        $tmpSrc     = pathinfo(strtolower($srcFile));
        $tmpDest    = pathinfo(strtolower($destFile));
        $size       = getimagesize($srcFile);
    
        if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
        {
            $destFile  = substr_replace($destFile, 'jpg', -3);
            $dest      = imagecreatetruecolor($w, $h);
            imageantialias($dest, TRUE);
        } elseif ($tmpDest['extension'] == "png") {
            $dest = imagecreatetruecolor($w, $h);
            imageantialias($dest, TRUE);
        } else {
            return false;
        }
        switch($size[2])
        {
            case 1:       //GIF
                $src = imagecreatefromgif($srcFile);
                break;
             case 2:       //JPEG
                $src = imagecreatefromjpeg($srcFile);
                break;
            case 3:       //PNG
                $src = imagecreatefrompng($srcFile);
                break;
            default:
                return false;
                 break;
        }
    
        imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
        switch($size[2])
        {
            case 1:
            case 2:
                imagejpeg($dest,$destFile, $quality);
                 break;
                case 3:
                imagepng($dest,$destFile);
        }
        return $destFile;
    }
#52

[eluser]Puspex[/eluser]
also add (sorry, my post was too long)

Code:
function cropImage($nw, $nh, $source, $stype, $dest) {
        $size = getimagesize($source);
        $w = $size[0];
        $h = $size[1];
        switch(strtolower($stype)) {
            case 'gif':
            $simg = imagecreatefromgif($source);
            break;
            case 'jpg':
            $simg = imagecreatefromjpeg($source);
            break;
            case 'png':
            $simg = imagecreatefrompng($source);
            break;
        }
        $dimg = imagecreatetruecolor($nw, $nh);
        $wm = $w/$nw;
        $hm = $h/$nh;
        $h_height = $nh/2;
        $w_height = $nw/2;
        if($w> $h) {
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;
            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
        } elseif(($w <$h) || ($w == $h)) {
            $adjusted_height = $h / $wm;
            $half_height = $adjusted_height / 2;
            $int_height = $half_height - $h_height;
            imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
        } else {
            imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }
        imagejpeg($dimg,$dest,100);
        
        return basename($dest);
    }

and change this
Code:
move_uploaded_file($file_temp,$targetFile);
to
Code:
$images = uploadImage('Filedata', $path);
   $mainImage = $images['image'];
   $thumbnail = $images['thumbnail'];

and add this
Code:
$filearray['file_image'] = $mainImage;
   $filearray['file_thumb'] = $thumbnail;
after line 85 (line 241 if u add the functions before this).

If u wish to print the image and thumb's name to the view, add this
Code:
<li>File Image: &lt;?php echo $json->{'file_image'};?&gt;</li>
        <li>File Thumbnail: &lt;?php echo $json->{'file_thumb'};?&gt;</li>
to the uploadify view.

Hope this helps anyone!

Cheers Smile
#53

[eluser]Bramme[/eluser]
wow. that is an awesome addition! thank you very much!

You got me thinking: is it actually possible to get an instance of CI in an outside php file? That way, you could just use ci's libraries.
#54

[eluser]eoinmcg[/eluser]
Hi Bramme,

I managed to get the latest version of uploadify working in CI without any changes to the flash and with the upload handled by my controller.

The only change I had to make was to include 'application/octet-stream' to mimes.php for the image types I want uploaded.

Give it a go with the latest version.


Cheers,
Eoin
#55

[eluser]Bramme[/eluser]
Another useful addition, thanks!
#56

[eluser]Adrian Walls[/eluser]
Hi folks,

I have trawled through the forums trying to find an solution to get uploadify working but nothing has work for me yet. I have implemented uploadify (multi file) into my CI app and have everything set-up as per the uploadify implementation instructions (or at least I thought I did). When I click on browse and select an image or images, uploadify shows a progress bar for each upload and indicates the upload has completed but when I check my upload directory its empty. I am running Firebug with Firephp but can't see any output at all. I would expect to see something here.

Rather than diluting this post with lots of code I'll show a few core areas for now.

Code:
$(document).ready(function() {
  $("#uploadify").uploadify({
    'uploader'       : 'www/js/uploadify/uploadify.swf',
    'script'         : 'www/js/uploadify/uploadify.php',
    'cancelImg'      : 'www/js/uploadify/cancel.png',
    'folder'         : 'www/gallery',
    'queueID'        : 'fileQueue',
    'auto'           : true,
    'multi'          : true
  });
});

Form Html (Form contains other input/select elements but have stripped them out here)
Code:
&lt;form name="stock_form" id="stock_form" action="index.php/admin/update_stock_item" method="post" enctype="multipart/form-data" &gt;
  <fieldset style="padding:20px;">
  <legend>Photos</legend>
  <p>Click browse to find your photos and upload.</p>
  
  <div id="fileQueue"></div>
    &lt;input type="file" name="uploadify" id="uploadify" /&gt;
    <p><a href="[removed]jQuery('#uploadify').uploadifyClearQueue()">Cancel All Uploads</a></p>
  </fieldset>
&lt;/form&gt;

Let me know if you need any other info. Tearing my hair out with this one and dont want it to completely beat me :-S Really like the look of this plugin and it would be brilliant if I could get it to work. Any help/suggestions much appreciated.
#57

[eluser]Puspex[/eluser]
Hey Wallzy!

In your view file where u set up the uploadify, should be using:

Code:
$(document).ready(function() {
  $("#uploadify").uploadify({
    'uploader'       : '&lt;?php echo base_url(); ?&gt;js/uploadify/uploadify.swf',
    'script'         : '&lt;?php echo base_url(); ?&gt;js/uploadify/uploadify.php',
    'cancelImg'      : '&lt;?php echo base_url(); ?&gt;js/uploadify/cancel.png',
    'folder'         : '&lt;?php echo base_url(); ?&gt;gallery',
    'queueID'        : 'fileQueue',
    'auto'           : true,
    'multi'          : true
  });
});

This way it should work!
#58

[eluser]Adrian Walls[/eluser]
Hi,

Thanks for your reply.

I am using the html &lt;base&gt; tag so everything should be relative to that. It's set to

Code:
&lt;base href="&lt;?php echo base_url(); ?&gt;" /&gt;
#59

[eluser]Puspex[/eluser]
Hey again!

The other problem could be that your path in the uploadify/uploadify.php isn't pointing to the right location...
You could check that... Mine is pointing to
Code:
$path = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';

Hope this helps!
#60

[eluser]Adrian Walls[/eluser]
Thanks Puspex, however it's set to be the same as yours.

I am at a bit of a dead end here and might just have to try a different upload solution.




Theme © iAndrew 2016 - Forum software by © MyBB