We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18436
    • 135 Posts
    Hi Guys, been a while since I’ve posted on the forums, still loving and pimping MODX but I’ve hit a brickwall mainly due to my lack of a deeper knowledge of PHP and MODX plugin architecture. Here’s the scenario: I came across the Multiphotos Plugin and it seems to be what I need for a difficult project. I’m now trying to add some additional image metadata that I need output in the Frontend but can’t for the life of me figure out how to do it via OnDocFormSave rather than via the frontend snippet.

    In essence I’m trying to inject the image dimensions back into the multi-dimensional array that’s being used by the foreach loop.
    The values that need to be injected should be v[3] (image width) with an additional v[4] (image height). $fotoArr is the json_decoded array.

    For performance purposes I’d prefer to inject these values when the data is saved, I have managed to run getImageSize() when outputing via the snippet but I think this will affect the server negatively due it being called by every page view rather than once when the document is saved.

    I’ve also tried adding debug messages using the Event Logger but nothing seems to work. If anybody could help out you’d be saving my life tremendously.

    if ($e->name == 'OnBeforeDocFormSave'){
    	$tvIds=explode(',',$tvIds);
    	foreach ($tvIds as $tvid) {
    		if (empty($th_width) && empty($th_height)) return;
    		if (!$resize || !isset($tmplvars[$tvid]) || empty($tmplvars[$tvid][1])) return;
    		$fotoArr=json_decode($tmplvars[$tvid][1]);
    		set_time_limit(0);
    		foreach ($fotoArr as $k=>&$v) {
    
    			// ATTEMPT TO MODIFY
    			$v[3]="test";
    
    			if (!empty($v[1]) && $auto_small) $v[0]=$v[1];
    			
    			if (!empty($v[0])){
    				$filename = basename($v[0]);
    				if (!$auto_small && $prefix==substr($filename, 0, strlen($prefix))) continue;
    				$dirname = str_replace($filename,'',$v[0]);
    				$new_path = '../'.$dirname.$prefix.$filename;
    				$imgInfo = @getImageSize('../'.$v[0]);
    				if (!is_array($imgInfo)) continue;
    				ob_start();
    				$img_width = $imgInfo[0];
    				$img_height = $imgInfo[1];
    				$width=$img_width;
    				$height=$img_height;
    
    				// ATTEMPT TO MODIFY
    
    				$fotoArr[$k][3]=$img_width;
    				$v[3]=$img_width;
    				
    				$posX=0;
    				$posY=0;	
    				$ratio = $img_height / $img_width;
    				if (!$th_height) $th_h=round($th_width*$ratio); else $th_h=$th_height;
    				if (!$th_width) $th_w=round($th_height/$ratio); else $th_w=$th_width;
    				$th_ratio = $th_h / $th_w;
    				if ($crop) {
    					if ($ratio > $th_ratio) {
    						$height=round($img_width*$th_ratio);
    						$posY=round(($img_height-$height)/2);
    					}
    					if ($ratio < $th_ratio) {
    						$width=round($img_height/$th_ratio);
    						$posX=round(($img_width-$width)/2);
    					}
    				}
    				else {
    					if ($ratio > $th_ratio) $th_w=round($th_h/$ratio);
    					if ($ratio < $th_ratio) $th_h=round($th_w*$ratio);
    				}
    				switch($imgInfo[2]){
    				case 1:
    					$src = ImageCreateFromGif('../'.$v[0]);
    					$dst = ImageCreateTrueColor($th_w, $th_h);
    					ImageCopyResampled($dst, $src, 0, 0, $posX, $posY, $th_w, $th_h, $width, $height);
    					ImageGif($dst,$new_path);
    					break;
    				case 2:
    					$src = ImageCreateFromJpeg('../'.$v[0]);
    					$dst = ImageCreateTrueColor($th_w, $th_h);
    					ImageCopyResampled($dst, $src, 0, 0, $posX, $posY, $th_w, $th_h, $width, $height);
    					ImageJpeg($dst,$new_path,90);
    					break;
    				case 3:
    					$src = ImageCreateFromPng('../'.$v[0]);
    					$dst = ImageCreateTrueColor($th_w, $th_h);
    					imagesavealpha($dst, true);
    					$cc=imagecolorallocatealpha($dst, 255, 255, 255, 127);
    					imagefill($dst, 0, 0, $cc); 
    					ImageCopyResampled($dst, $src, 0, 0, $posX, $posY, $th_w, $th_h, $width, $height);
    					ImagePng($dst,$new_path);
    					break;
    				}
    				imagedestroy($src);
    				imagedestroy($dst);
    				if (empty($v[1]) && $auto_big) $v[1]=$v[0];
    				$v[0]=$dirname.$prefix.$filename;
    				ob_end_clean();
    			}
    		}
    		$tmplvars[$tvid][1]=str_replace('\\/', '/', json_encode($fotoArr));
    	}
    }
    


    Returned after running json_decode on the template variable value

    Array ( [0] => Array ( [0] => assets/images/portfolio/yarrsome screengrab.jpg [1] => assets/images/portfolio/yarrsome screengrab.jpg [2] => title 02 [3] => 125 ) [1] => Array ( [0] => assets/images/portfolio/people/people-01/giovanni/front1.jpg [1] => assets/images/portfolio/people/people-01/giovanni/front1.jpg [2] => test [3] => ) ) 
    


    Raw TV Value
    [["assets/images/portfolio/yarrsome screengrab.jpg","assets/images/portfolio/yarrsome screengrab.jpg","title 02",""],["assets/images/portfolio/people/people-01/giovanni/front1.jpg","assets/images/portfolio/people/people-01/giovanni/front1.jpg","test",""]]
    
      • 29487
      • 385 Posts
      Hmm...
      Variable values must be stored in hidden fields, otherwise they will lose when editing.
      If "auto_small" not enabled, code will be executed only once.