1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Helper_Image extends Mage_Core_Helper_Abstract
35: {
36: 37: 38:
39: const XMLCONNECT_GLUE = '_';
40:
41: 42: 43: 44: 45:
46: protected $_content = null;
47:
48: 49: 50: 51: 52:
53: protected $_interface = null;
54:
55: 56: 57: 58: 59:
60: protected $_interfacePath = array();
61:
62: 63: 64: 65: 66:
67: protected $_imageLimits = array();
68:
69: 70: 71: 72: 73:
74: protected $_confPaths = null;
75:
76: 77: 78: 79: 80: 81: 82:
83: public function handleUpload($field)
84: {
85: $uploadedFilename = '';
86: $uploadDir = $this->getOriginalSizeUploadDir();
87:
88: try {
89: $this->_forcedConvertPng($field);
90:
91:
92: $uploader = Mage::getModel('core/file_uploader', $field);
93: $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
94: $uploader->setAllowRenameFiles(true);
95: $uploader->save($uploadDir);
96: $uploadedFilename = $uploader->getUploadedFileName();
97: $uploadedFilename = $this->_getResizedFilename($field, $uploadedFilename, true);
98: } catch (Exception $e) {
99: 100: 101:
102: if (!strlen($_FILES[$field]['tmp_name'])) {
103: Mage::throwException(Mage::helper('xmlconnect')->__('File can\'t be uploaded.'));
104: } elseif ($e->getMessage() == 'Disallowed file type.') {
105: $filename = $_FILES[$field]['name'];
106: Mage::throwException(
107: Mage::helper('xmlconnect')->__('Error while uploading file "%s". Disallowed file type. Only "jpg", "jpeg", "gif", "png" are allowed.', $filename)
108: );
109: } else {
110: Mage::logException($e);
111: }
112: }
113: return basename($uploadedFilename);
114: }
115:
116: 117: 118: 119: 120:
121: protected function _getScreenSize()
122: {
123: return Mage::helper('xmlconnect')->getApplication()->getScreenSize();
124: }
125:
126: 127: 128: 129: 130: 131: 132: 133: 134:
135: protected function _getResizedFilename($fieldPath, $fileName, $default = false)
136: {
137: $fileName = basename($fileName);
138: if ($default) {
139: $dir = $this->getDefaultSizeUploadDir();
140: } else {
141: $dir = $this->getCustomSizeUploadDir($this->_getScreenSize());
142: }
143: $customSizeFileName = $dir . DS . $fileName;
144: $originalSizeFileName = $this->getOriginalSizeUploadDir(). DS . $fileName;
145:
146: 147: 148:
149: if (!file_exists($originalSizeFileName)) {
150: $oldFileName = $this->getOldUploadDir() . DS . $fileName;
151: if (file_exists($oldFileName)) {
152: if (!(copy($oldFileName, $originalSizeFileName)
153: && (is_readable($customSizeFileName) || chmod($customSizeFileName, 0644))
154: )) {
155: Mage::throwException(
156: Mage::helper('xmlconnect')->__('Error while processing file "%s".', $fileName)
157: );
158: }
159: } else {
160: Mage::throwException(Mage::helper('xmlconnect')->__('No such file "%s".', $fileName));
161: }
162: }
163:
164: $isCopied = copy($originalSizeFileName, $customSizeFileName);
165: clearstatcache();
166: if ($isCopied && (is_readable($customSizeFileName) || chmod($customSizeFileName, 0644))) {
167: $this->_handleResize($fieldPath, $customSizeFileName);
168: } else {
169: $fileName = '';
170: if (isset($_FILES[$fieldPath]) && is_array($_FILES[$fieldPath]) && isset($_FILES[$fieldPath]['name'])) {
171: $fileName = $_FILES[$fieldPath]['name'];
172: }
173: Mage::throwException(Mage::helper('xmlconnect')->__('Error while uploading file "%s".', $fileName));
174: }
175: return $customSizeFileName;
176: }
177:
178: 179: 180: 181: 182: 183: 184:
185: protected function _handleResize($fieldPath, $file)
186: {
187: $nameParts = explode('/', $fieldPath);
188: array_shift($nameParts);
189: $conf = $this->getInterfaceImageLimits();
190: while (count($nameParts)) {
191: $next = array_shift($nameParts);
192: if (isset($conf[$next])) {
193: $conf = $conf[$next];
194: } else {
195: 196: 197:
198: return;
199: }
200: }
201:
202: $image = new Varien_Image($file);
203: $width = $image->getOriginalWidth();
204: $height = $image->getOriginalHeight();
205:
206: if (isset($conf['widthMax']) && ($conf['widthMax'] < $width)) {
207: $width = $conf['widthMax'];
208: } elseif (isset($conf['width'])) {
209: $width = $conf['width'];
210: }
211:
212: if (isset($conf['heightMax']) && ($conf['heightMax'] < $height)) {
213: $height = $conf['heightMax'];
214: } elseif (isset($conf['height'])) {
215: $height = $conf['height'];
216: }
217:
218: if (($width != $image->getOriginalWidth()) || ($height != $image->getOriginalHeight())) {
219: $image->keepTransparency(true);
220: $image->keepFrame(true);
221: $image->keepAspectRatio(true);
222: $image->backgroundColor(array(255, 255, 255));
223: $image->resize($width, $height);
224: $image->save(null, basename($file));
225: }
226: }
227:
228: 229: 230: 231: 232:
233: protected function _forcedConvertPng($field)
234: {
235: $file =& $_FILES[$field];
236:
237: $dotPosition = strrpos($file['name'], '.');
238: if ($dotPosition !== false) {
239: $file['name'] = substr($file['name'], 0 , $dotPosition);
240: }
241: $file['name'] .= '.png';
242:
243:
244:
245: list($unnecessaryVar, $unnecessaryVar, $fileType) = getimagesize($file['tmp_name']);
246: unset($unnecessaryVar);
247:
248: if ($fileType != IMAGETYPE_PNG) {
249: switch ($fileType) {
250: case IMAGETYPE_GIF:
251: $img = imagecreatefromgif($file['tmp_name']);
252: imagealphablending($img, false);
253: imagesavealpha($img, true);
254: break;
255: case IMAGETYPE_JPEG:
256: $img = imagecreatefromjpeg($file['tmp_name']);
257: break;
258: case IMAGETYPE_WBMP:
259: $img = imagecreatefromwbmp($file['tmp_name']);
260: break;
261: case IMAGETYPE_XBM:
262: $img = imagecreatefromxbm($file['tmp_name']);
263: break;
264: default:
265: return;
266: }
267: imagepng($img, $file['tmp_name']);
268: imagedestroy($img);
269: }
270: }
271:
272: 273: 274: 275: 276: 277:
278: public function getSkinImagesUrl($name = null)
279: {
280: return Mage::getDesign()->getSkinUrl('images/xmlconnect/' . $name);
281: }
282:
283: 284: 285: 286: 287:
288: public function getCustomSizeDirPrefix()
289: {
290: return $this->_getScreenSize() . DS . 'custom';
291: }
292:
293: 294: 295: 296: 297: 298:
299: public function getFileDefaultSizeSuffixAsUrl($fileName)
300: {
301: return 'custom' . '/' . $this->_getScreenSize() . '/' . basename($fileName);
302: }
303:
304: 305: 306: 307: 308: 309: 310:
311: public function getFileCustomDirSuffixAsUrl($confPath, $fileName)
312: {
313: return 'custom'
314: . '/'
315: . $this->_getScreenSize()
316: . '/'
317: . basename($this->_getResizedFilename($confPath, $fileName));
318: }
319:
320: 321: 322: 323: 324: 325:
326: public function getImageSizeForContent($imageName)
327: {
328: if (!isset($this->_content)) {
329: $imageLimits = $this->getImageLimits($this->_getScreenSize());
330: if (($imageLimits['content']) && is_array($imageLimits['content'])) {
331: $this->_content = $imageLimits['content'];
332: } else {
333: $this->_content = array();
334: }
335: }
336: $size = isset($this->_content[$imageName]) ? (int) $this->_content[$imageName] : 0;
337: return $size;
338: }
339:
340: 341: 342: 343: 344:
345: public function getInterfaceImageLimits()
346: {
347: if (!isset($this->_interface)) {
348: $imageLimits = $this->getImageLimits($this->_getScreenSize());
349: $this->_interface = $imageLimits['interface'];
350: }
351: return $this->_interface;
352: }
353:
354: 355: 356: 357: 358: 359:
360: public function getImageSizeForInterface($imagePath)
361: {
362: if (!isset($this->_interfacePath[$imagePath])) {
363:
364: $app = Mage::helper('xmlconnect')->getApplication();
365: if (!$app) {
366: return 0;
367: } else {
368: $imageLimits = $this->getImageLimits($this->_getScreenSize());
369: $size = $this->findPath($imageLimits, $imagePath);
370: $this->_interfacePath[$imagePath] = $size;
371: }
372: }
373: $size = isset($this->_interfacePath[$imagePath]) ? (int) $this->_interfacePath[$imagePath] : 0;
374: return $size;
375: }
376:
377: 378: 379: 380: 381: 382:
383: public function getMediaPath($path = '')
384: {
385: $path = trim($path);
386: $result = Mage::getBaseDir('media') . DS . 'xmlconnect';
387:
388: if (!empty($path)) {
389: if (strpos($path, DS) === 0) {
390: $path = substr($path, 1);
391: }
392: $result .= DS . $path;
393: }
394: return $result;
395: }
396:
397: 398: 399: 400: 401: 402:
403: public function getMediaUrl($image = '')
404: {
405: $image = trim($image);
406: $result = Mage::getBaseUrl('media') . 'xmlconnect';
407:
408: if (!empty($image)) {
409: if (strpos($image, '/') === 0) {
410: $image = substr($image, 1);
411: }
412: $result .= '/' . $image;
413: }
414: return $result;
415: }
416:
417: 418: 419: 420: 421: 422:
423: public function getDefaultDesignUrl($image = '')
424: {
425: return $this->getSkinImagesUrl($this->getDefaultDesignSuffixAsUrl($image));
426: }
427:
428: 429: 430: 431: 432: 433:
434: public function getDefaultDesignSuffixAsUrl($image = '')
435: {
436: return 'design_default/' . trim(ltrim($image, '/'));
437: }
438:
439: 440: 441: 442: 443: 444: 445: 446:
447: public function getCustomSizeImageUrl($imageUrl, $width = 100, $height = 100)
448: {
449: $screenSize = $width . 'x' . $height;
450: $customDir = $this->getMediaPath('custom' . DS . $screenSize);
451: $this->_verifyDirExist($customDir);
452: $imageUrl = explode('/', $imageUrl);
453: $file = $imageUrl[count($imageUrl)-1];
454: $filePath = $this->getDefaultSizeUploadDir() . DS . $file;
455: if (!file_exists($customDir . DS . $file)) {
456: $image = new Varien_Image($filePath);
457: $widthOriginal = $image->getOriginalWidth();
458: $heightOriginal = $image->getOriginalHeight();
459:
460: if ($width != $widthOriginal) {
461: $widthOriginal = $width;
462: }
463:
464: if ($height != $heightOriginal) {
465: $heightOriginal = $height;
466: }
467:
468: if (($widthOriginal != $image->getOriginalWidth()) ||
469: ($heightOriginal != $image->getOriginalHeight()) ) {
470: $image->keepTransparency(true);
471: $image->keepFrame(true);
472: $image->keepAspectRatio(true);
473: $image->backgroundColor(array(255, 255, 255));
474: $image->resize($widthOriginal, $heightOriginal);
475: $image->save($customDir, basename($file));
476: }
477: }
478: return $this->getMediaUrl("custom/{$screenSize}/" . basename($file));
479: }
480:
481: 482: 483: 484: 485: 486:
487: public function filterScreenSize($screenSize)
488: {
489: $screenSize = preg_replace('/[^0-9A-z_]/', '', $screenSize);
490: if (isset($this->_imageLimits[$screenSize])) {
491: return $screenSize;
492: }
493: $screenSizeExplodeArray = explode(self::XMLCONNECT_GLUE, $screenSize);
494: $version = '';
495: switch (count($screenSizeExplodeArray)) {
496: case 2:
497: $version = $screenSizeExplodeArray[1];
498: case 1:
499: $resolution = $screenSizeExplodeArray[0];
500: break;
501: default:
502: $resolution = Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_DEFAULT;
503: break;
504: }
505:
506: $sourcePath = empty($version) ? Mage_XmlConnect_Model_Application::APP_SCREEN_SOURCE_DEFAULT : $version;
507: $xmlPath = 'screen_size/' . self::XMLCONNECT_GLUE . $resolution . '/' . $sourcePath . '/source';
508:
509: $source = Mage::getStoreConfig($xmlPath);
510: if (!empty($source)) {
511: $screenSize = $resolution . (empty($version) ? '' : self::XMLCONNECT_GLUE . $version);
512: } else {
513: $screenSize = Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_DEFAULT;
514: }
515: return $screenSize;
516: }
517:
518: 519: 520: 521: 522: 523:
524: public function getImageLimits($screenSize = Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_DEFAULT)
525: {
526: $defaultScreenSize = Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_DEFAULT;
527: $defaultScreenSource = Mage_XmlConnect_Model_Application::APP_SCREEN_SOURCE_DEFAULT;
528:
529: $screenSize = preg_replace('/[^0-9A-z_]/', '', $screenSize);
530: if (isset($this->_imageLimits[$screenSize])) {
531: return $this->_imageLimits[$screenSize];
532: }
533:
534: $screenSizeExplodeArray = explode(self::XMLCONNECT_GLUE, $screenSize);
535: $version = '';
536: switch (count($screenSizeExplodeArray)) {
537: case 2:
538: $version = $screenSizeExplodeArray[1];
539: case 1:
540: $resolution = $screenSizeExplodeArray[0];
541: break;
542: default:
543: $resolution = $defaultScreenSize;
544: break;
545: }
546:
547: $sourcePath = empty($version) ? $defaultScreenSource : $version;
548: $xmlPath = 'screen_size/' . self::XMLCONNECT_GLUE . $resolution . '/' . $sourcePath;
549:
550: $root = Mage::getStoreConfig($xmlPath);
551: $updates = array();
552:
553: if (!empty($root)) {
554: $screenSize = $resolution . (empty($version) ? '' : self::XMLCONNECT_GLUE . $version);
555: $source = !empty($root['source']) ? $root['source'] : $defaultScreenSource;
556: $updates = isset($root['updates']) && is_array($root['updates']) ? $root['updates'] : array();
557: } else {
558: $screenSize = $defaultScreenSize;
559: $source = $defaultScreenSource;
560: }
561:
562: $imageLimits = Mage::getStoreConfig('screen_size/' . $source);
563: if (!is_array($imageLimits)) {
564: $imageLimits = Mage::getStoreConfig('screen_size/default');
565: }
566:
567: foreach ($updates as $update) {
568: $path = $update['path'];
569: $function = $update['function'];
570: switch ($function) {
571: case 'zoom':
572: $data = $update['data'];
573: $target =& $this->findPath($imageLimits, $path);
574: if (is_array($target)) {
575: array_walk_recursive($target, array($this, '_zoom'), $data);
576: } else {
577: $this->_zoom($target, null, $data);
578: }
579: break;
580: case 'update':
581: $data = $update['data'];
582: $target =& $this->findPath($imageLimits, $path);
583: $target = $data;
584: break;
585: case 'insert':
586: $data = $update['data'];
587: $target =& $this->findPath($imageLimits, $path);
588: if (($target !== null) && (is_array($target)) && (is_array($data))) {
589: foreach ($data as $key => $val) {
590: $target[$key] = $val;
591: }
592: }
593: break;
594: case 'delete':
595: $data = $update['data'];
596: $target =& $this->findPath($imageLimits, $path);
597: if (isset($target[$data])) {
598: unset($target[$data]);
599: }
600: break;
601: default:
602: break;
603: }
604:
605: }
606: if (!is_array($imageLimits)) {
607: $imageLimits = array();
608: }
609:
610: $this->_imageLimits[$screenSize] = $imageLimits;
611: return $imageLimits;
612: }
613:
614: 615: 616: 617: 618: 619: 620:
621: public function &findPath(&$array, $path)
622: {
623: $target =& $array;
624: if ($path !== '/') {
625: $pathArray = explode('/', $path);
626: foreach ($pathArray as $node) {
627: if (is_array($target) && isset($target[$node])) {
628: $target =& $target[$node];
629: } else {
630: $targetNull = null;
631: return $targetNull;
632: }
633: }
634: }
635: return $target;
636: }
637:
638: 639: 640: 641: 642: 643: 644: 645:
646: protected function _zoom(&$item, $key, $value)
647: {
648: if (is_string($item)) {
649: $item = (int) round($item * $value);
650: }
651: }
652:
653: 654: 655: 656: 657: 658:
659: protected function _verifyDirExist($dir)
660: {
661: try {
662: $ioFile = new Varien_Io_File();
663: $ioFile->checkAndCreateFolder($dir);
664: } catch (Exception $e) {
665: Mage::throwException($e->getMessage());
666: }
667: }
668:
669: 670: 671: 672: 673: 674:
675: public function getCustomSizeUploadDir($screenSize)
676: {
677: $screenSize = $this->filterScreenSize($screenSize);
678: $customDir = $this->getMediaPath('custom' . DS . $screenSize);
679: $this->_verifyDirExist($customDir);
680: return $customDir;
681: }
682:
683: 684: 685: 686: 687:
688: public function getOriginalSizeUploadDir()
689: {
690: $dir = $this->getMediaPath('original');
691: $this->_verifyDirExist($dir);
692: return $dir;
693: }
694:
695: 696: 697: 698: 699:
700: public function getOldUploadDir()
701: {
702: $dir = $this->getMediaPath();
703: $this->_verifyDirExist($dir);
704: return $dir;
705: }
706:
707: 708: 709: 710: 711:
712: public function getDefaultSizeUploadDir()
713: {
714: return $this->getCustomSizeUploadDir(Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_DEFAULT);
715: }
716:
717: 718: 719: 720: 721:
722: public function getInterfaceImagesPathsConf()
723: {
724: if (!isset($this->_confPaths)) {
725: $this->_confPaths = array();
726: $paths = $this->getInterfaceImagesPaths();
727: if (is_array($paths)) {
728: $len = strlen('conf/native/');
729: while (list($path,) = each($paths)) {
730: $this->_confPaths[$path] = substr($path, $len);
731: }
732: }
733: }
734: return $this->_confPaths;
735: }
736:
737: 738: 739: 740: 741: 742: 743: 744:
745: public function getInterfaceImagesPaths($imagePath = null)
746: {
747: $paths = array (
748: 'conf/native/navigationBar/icon' => 'smallIcon_1_6.png',
749: 'conf/native/body/bannerImage' => 'banner_1_2.png',
750: 'conf/native/body/bannerIpadLandscapeImage' => 'banner_ipad_l.png',
751: 'conf/native/body/bannerIpadImage' => 'banner_ipad.png',
752: 'conf/native/body/bannerAndroidImage' => 'banner_android.png',
753: 'conf/native/body/backgroundImage' => 'accordion_open.png',
754: 'conf/native/body/backgroundIpadLandscapeImage' => 'accordion_open_ipad_l.png',
755: 'conf/native/body/backgroundIpadPortraitImage' => 'accordion_open_ipad_p.png',
756: 'conf/native/body/backgroundAndroidLandscapeImage' => 'accordion_open_android_l.png',
757: 'conf/native/body/backgroundAndroidPortraitImage' => 'accordion_open_android_p.png',
758: );
759: if ($imagePath == null) {
760: return $paths;
761: } else if (isset($paths[$imagePath])) {
762: return $paths[$imagePath];
763: } else {
764: return null;
765: }
766: }
767:
768: 769: 770: 771: 772: 773:
774: public function checkAndGetImagePath(&$icon)
775: {
776: $icon = basename($icon);
777: if (is_file($this->getDefaultSizeUploadDir() . DS . $icon)) {
778: $icon = $this->getDefaultSizeUploadDir() . DS . $icon;
779: return true;
780: }
781: return false;
782: }
783: }
784: