Created
March 31, 2015 13:37
-
-
Save michaelwilhelmsen/58eb83f8f1d6f1f94a42 to your computer and use it in GitHub Desktop.
Whole Zip file with all relevant functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Custom Functions for Screenpartners Bildebank | |
| */ | |
| /****************************************************** | |
| * TASK: Create LOW RES duplicates of uploaded images, * | |
| * create alternate download button for these files * | |
| * "Download Files" / "Download Low Res Files" * | |
| ******************************************************/ | |
| /* Directory Location of WP Download Manager's Uploads | |
| ** All wpdm files uploads to this directory */ | |
| function get_full_upload_dir() { | |
| $uploads = wp_upload_dir(); | |
| return $uploads['baseurl'] . '/download-manager-files/'; | |
| } | |
| function get_lres_full_upload_dir() { | |
| $uploads = wp_upload_dir(); | |
| return $uploads['baseurl'] . '/lres-download-manager-files/'; | |
| } | |
| // Function that returns an array of file paths to the relevant wpdm package | |
| function uploaded_files_path() { | |
| $identity = get_the_ID(); | |
| $files = get_package_data($identity, 'files'); | |
| $file_list = array(); | |
| // Adds each file path to $file_list array | |
| foreach ($files as $file) { | |
| $file_path = UPLOAD_DIR . $file; | |
| $file_list[] = $file_path; | |
| } | |
| return $file_list; | |
| } | |
| // Function to create low res versions of images - returns an array with their path | |
| function resize_images() { | |
| $file_list = uploaded_files_path(); | |
| $low_res_image_path_list=array(); | |
| foreach ($file_list as $file) { | |
| $imagick = new Imagick(); | |
| $img = wp_get_image_editor($file); | |
| $imagick->readImage($file); | |
| if ( ! is_wp_error( $imagick ) ) { | |
| $filenamelres = $img->generate_filename('low-res', ABSPATH.'wp-content/uploads/lres-download-manager-files/', NULL ); | |
| if (!file_exists($filenamelres)) { | |
| $imagick->setImageResolution(50,50); | |
| $imagick->resampleImage(50,50,imagick::FILTER_UNDEFINED,0); | |
| $imagick->resizeImage( 600, 0, Imagick::FILTER_UNDEFINED, 1 ); | |
| $imagick->writeImage($filenamelres); | |
| $low_res_image_path_list[] = $filenamelres; | |
| } else { | |
| $filenamelres = $img->generate_filename('low-res', ABSPATH.'wp-content/uploads/lres-download-manager-files/', NULL ); | |
| $low_res_image_path_list[] = $filenamelres; | |
| } | |
| } else { | |
| // For testing | |
| echo 'ERROR! File ' . $count . ' might not be recognized as an image!'; | |
| echo '<br>'; | |
| } | |
| $imagick->destroy(); | |
| } | |
| return $low_res_image_path_list; | |
| } | |
| function Zip($source, $destination) { | |
| echo "called function"; | |
| if (!extension_loaded('zip') || !file_exists($source)) { | |
| return false; | |
| } | |
| $zip = new ZipArchive(); | |
| if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { | |
| return false; | |
| } | |
| $source = str_replace('\\', '/', realpath($source)); | |
| if (is_dir($source) === true) | |
| { | |
| $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); | |
| foreach ($files as $file) | |
| { | |
| $file = str_replace('\\', '/', $file); | |
| // Ignore "." and ".." folders | |
| if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) | |
| continue; | |
| $file = realpath($file); | |
| if (is_dir($file) === true) | |
| { | |
| $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); | |
| } | |
| else if (is_file($file) === true) | |
| { | |
| $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); | |
| } | |
| } | |
| } | |
| else if (is_file($source) === true) | |
| { | |
| $zip->addFromString(basename($source), file_get_contents($source)); | |
| } | |
| return $zip->close(); | |
| } | |
| // Function to create a download link for the created zip archive | |
| function lres_download_link() { | |
| // Testing | |
| $resize_images = resize_images(); | |
| $lres_directory = get_lres_full_upload_dir() . get_the_title() . '-low-res.zip'; | |
| $filename = create_zip_lres($resize_images, $lres_directory); | |
| echo '<a href="' . $filename . '" alt="Download low res zip">Download Low res</a>'; | |
| } |
Author
michaelwilhelmsen
commented
Mar 31, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment