%5E)... if (isset ($_GET['skus'])) { $skus = filter_var (urldecode ($_GET['skus']), FILTER_SANITIZE_STRING); $skus = explode ('^', $skus); } else { $skus = false; return false; } // $skus is now like: // $skus = array { // 0 => 'UsMilan', // 1 => 'KrQuadro', // } // Get maxSize... if (isset ($_GET['maxsize'])) { $maxSize = filter_var (urldecode ($_GET['maxsize']), FILTER_SANITIZE_NUMBER_INT); $maxSize = max ($minSize, $maxSize); $maxWidth = $maxSize; $maxHeight = $maxSize; } // Get maxWidth... if (isset ($_GET['maxwidth'])) { $maxWidth = filter_var (urldecode ($_GET['maxwidth']), FILTER_SANITIZE_NUMBER_INT); $maxWidth = max ($minSize, $maxWidth); } // Get maxHeight... if (isset ($_GET['maxheight'])) { $maxHeight = filter_var (urldecode ($_GET['maxheight']), FILTER_SANITIZE_NUMBER_INT); $maxHeight = max ($minSize, $maxHeight); } function getFileUrlsInFolder ($sku, $localSourceFolderRoot, $allDownloadFileUrls) { $scanFolder = $localSourceFolderRoot . '/' . strtolower ($sku); $allDownloadFileUrls = array (); if (file_exists ($scanFolder)) { if (is_dir ($scanFolder)) { $filesInThisFolder = scandir ($scanFolder); if ($filesInThisFolder) { foreach ($filesInThisFolder as $thisFile) { if ($thisFile <> '.' and $thisFile <> '..') { array_push ($allDownloadFileUrls, $localSourceFolderRoot . '/' . strtolower ($sku) . '/' . $thisFile); } } } } } return $allDownloadFileUrls; } function getFileNamesToZip ($localDestFolder) { $filesInThisFolder = scandir ($localDestFolder); $filesToZip = false; if ($filesInThisFolder) { $filesToZip = array (); foreach ($filesInThisFolder as $thisFile) { if ($thisFile <> '.' and $thisFile <> '..') { array_push ($filesToZip, $localDestFolder . '/' . $thisFile); } } } return $filesToZip; } // Get an array holding the url's of all files to download including local path... // Like: // // allDownloadFileUrls = array ( // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/3BudgetPens_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/3Pens_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/Penondesk_blue_sf_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/Penondesk_red_sf_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/budget-pen_black_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/budget-pen_blue_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/pebudget/budget-pen_red_HR.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/usmilan/MILAN-500x500.jpg", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/usmilan/Milan Black.png", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/usmilan/Milan Dark Blue.png", // "/home/admin/domains/deonet.com/private_html/pub/media/downloads/usmilan/Milan Gold.png" // ); function getAllFileUrls ($skus, $localSourceFolderRoot) { $allDownloadFileUrls = array (); if ($skus) { foreach ($skus as $thisSku) { $allDownloadFileUrls = getFileUrlsInFolder ($thisSku, $localSourceFolderRoot, $allDownloadFileUrls); } } if (count ($allDownloadFileUrls) < 1) { $allDownloadFileUrls = false; } return $allDownloadFileUrls; } // $source like: '/home/admin/domains/prelive.deonet.com/public_html/images/test-hj-animated-gif.gif'... // $destination like: '/home/admin/domains/prelive.deonet.com/public_html/generated-downloads/scaled-1493022604-test-hj-animated-gif.gif'... // $source must be: .gif, .jpg or .png... // The resize image will also be sharpened, interlaced and compressed... function copyResizeImage ($source, $localDestFolder, $maxWidth, $maxHeight) { $fileName = afterLast ('/', $source); $destination = $localDestFolder . '/' . $fileName; $imageSize = getimagesize($source); // $imageSize array like: // 0 -> 1890, // 1 -> 3888, // 2 -> 2, // 3 -> "width='1890' height='2888'", // bits -> 8 // channels -> 3, // mime -> "image/jpeg" $aspectRatio = $imageSize[0] / $imageSize[1]; if ($maxWidth and !$maxHeight) { $maxHeight = $maxWidth / $aspectRatio; } elseif (!$maxWidth and $maxHeight) { $maxWidth = $maxHeight * $aspectRatio; } $maxWidthHeight = $maxWidth . 'x' . $maxHeight; // The resize stuff... $output = array(); $retVal = false; // Default values for sharpening and compressing... $sharpen = '0x1.0'; $quality = 85; $quality .= '%'; // Remark: // I have seen that some filenames have spaces in them... // Therefor I put double quotes (") around $source and $destination... // Resize... exec ('/bin/convert "' . $source . '" -resize ' . $maxWidthHeight . ' "' . $destination . '"', $output, $retVal); if ($retVal === 0) { // Sharpen... exec ('/bin/convert "' . $destination . '" -sharpen ' . $sharpen . ' "' . $destination . '"', $output, $retVal); if ($retVal === 0) { // Compress... exec ('/bin/convert -strip -interlace Plane -quality ' . $quality . ' "' . $destination . '" "' . $destination . '"', $output, $retVal); if ($retVal === 0) { return true; } } } else { echo '
', 'Could not resize: ', $source, '
'; echo '', 'dest:', $destination, '
'; echo '', '=================================================================', '
'; } return false; } function copyAllFilesToTempFolder ($allDownloadFileUrls, $localDestFolder, $maxWidth, $maxHeight) { if ($allDownloadFileUrls and $localDestFolder) { if (!file_exists ($localDestFolder)) { mkdir ($localDestFolder, 0777); } foreach ($allDownloadFileUrls as $thisDownloadFileUrl) { $fileName = afterLast ('/', $thisDownloadFileUrl); $extension = afterLast ('.', $fileName); // If $maxSize is set, then we have to resize images... if ($maxWidth or $maxHeight) { switch (strtolower ($extension)) { case 'jpg': case 'jpeg': case 'png': case 'gif': copyResizeImage ($thisDownloadFileUrl, $localDestFolder, $maxWidth, $maxHeight); break; default: $source = $thisDownloadFileUrl; $dest = $localDestFolder . '/' . $fileName; // Copy source-file to destination... copy ($source, $dest); } } else { $source = $thisDownloadFileUrl; $dest = $localDestFolder . '/' . $fileName; // Copy source-file to destination... copy ($source, $dest); } } return $localDestFolder; } return false; } // Creates a compressed zip file... // // $files should be an array like: // $files = array ( // 0 => '/home/admin/domains/deonet.com/private_html/pub/media/downloads/generated-downloads/1510046731/3BudgetPens_HR.jpg', // 1 => '/home/admin/domains/deonet.com/private_html/pub/media/downloads/generated-downloads/1510046731/new-product.csv]', // 2 => '/home/admin/domains/deonet.com/private_html/pub/media/downloads/generated-downloads/1510046731/template-shake-lighter-en.pdf]', // ); // // $files can contain images, csv-files, pdf-files etc... // // The number (1510046731 in this example) is a timestamp for a temp folder and/or file... // // $localZipFile should be like: "/home/admin/domains/deonet.com/private_html/pub/media/downloads/generated-downloads/1510046731/deonet-1510046731.zip"... // // If $overwrite == true, then an existing file with the same location/name will be overwritten... // otherwise no zip-archive will be created... // // This function returns true if the zip-archive is succesfully created... // otherwise false is returned... function createZip ($files = array(), $localZipFile = '', $overwrite = true) { // If the zip file already exists and overwrite is false, return false... if (file_exists ($localZipFile) && !$overwrite) { return false; } $validFiles = array(); // If files were passed in... if (is_array ($files)) { // Cycle through each file... foreach ($files as $thisFile) { // Make sure the file exists... if (file_exists ($thisFile)) { $validFiles[] = $thisFile; } } } // If we have valid files... if (count ($validFiles)) { // Create the zip archive... $zip = new ZipArchive(); if ($zip->open($localZipFile, ZipArchive::CREATE) !== true) { unset ($validFiles, $zip); return false; } // Add the files to the zip archive... foreach ($validFiles as $file) { $fileNameInZip = basename($file); $zip->addFile($file, $fileNameInZip); } // Close the zip archive... $zip->close(); unset ($validFiles, $file, $zip, $fileNameInZip); // Check to make sure the file exists... return file_exists ($localZipFile); } else { unset ($validFiles); return false; } } function isFolderEmpty ($folder) { return ($files = scandir($folder) and count($files) <= 2); } function deleteTempFiles ($localDestFolder) { if (file_exists ($localDestFolder)) { // Delete files in temp folder... $filesInThisFolder = scandir ($localDestFolder); if (count ($filesInThisFolder) > 2) { foreach ($filesInThisFolder as $thisFile) { if ($thisFile <> '.' and $thisFile <> '..') { unlink ($localDestFolder . '/' . $thisFile); } } } // Delete temp folder... if (isFolderEmpty ($localDestFolder)) { rmdir ($localDestFolder); } } return; } function sendZipToBrowser ($localZipFile) { if (file_exists ($localZipFile)) { // Get the zip-file and send it to the browser... header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($localZipFile) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($localZipFile)); readfile($localZipFile); } else { echo '', 'Not found: ', $localZipFile, '
'; } } // Create an array holding all filenames incl. local path that we have to put in a zip-file... $allDownloadFileUrls = getAllFileUrls ($skus, $localSourceFolderRoot); // Copy all files to a temp folder... if (!copyAllFilesToTempFolder ($allDownloadFileUrls, $localDestFolder, $maxWidth, $maxHeight)) { echo '', 'No files found for the given skus...', '
'; return false; } // Get an array holding all filenames incl. local path in temp folder... if (!$filesToZip = getFileNamesToZip ($localDestFolder)) { echo '', 'No files found in: temp folder...', '
'; return false; } // Create the zip-file... if (createZip ($filesToZip, $localZipFile)) { // Send the zip-file to the browser... sendZipToBrowser ($localZipFile); // Delete temp files and temp folder... deleteTempFiles ($localDestFolder); exit(0); } else { echo '', 'No zip-file created...', '
'; } ?>