Open Graph , set image ordering

Phoca plugins - support for all Phoca plugins except Phoca Gallery plugins
samuelsky
Phoca Newbie
Phoca Newbie
Posts: 1
Joined: 27 Dec 2017, 12:16

Open Graph , set image ordering

Post by samuelsky » 27 Dec 2017, 12:27

Ahoj Jan!
I had a view on the og-plugin.

Sometimes people like to work with very specific Preview-Pictures that are not included in the article.

For this case you offered the possibility to upload a picture with articleid.jpg to the folder images/phocaopengraph

This is a very smart solution. I like this!

My problem: I cant use this, if there is any other picture in my article.
Because in the priority ordering - taking the og:image from images/phocaopengraph is behind all the others.

I would like to set this behaviour first. First of all, the plugin should watch if there is a picture in images/phocaopengraph , if there is no picture it can have a look for all the other possibilities.

Could you please help me, to set the ordering in the right way?

Code: Select all

// Image
		$pictures = '';
		if (isset($row->images)) {
			$pictures = json_decode($row->images);
		}

		$imgSet = 0;
		
		if ($this->params->get('image'.$suffix, '') != '' && $parameterImage == 1) {
			$this->renderTag('og:image', JURI::base(false).$this->params->get('image'.$suffix, ''), $type);
			$imgSet = 1;
		} else if ($thisImg != ''){
			$this->renderTag('og:image', JURI::base(false).$thisImg, $type);
			$imgSet = 1;
		} else if (isset($pictures->{'image_intro'}) && $pictures->{'image_intro'} != '') {
			$this->renderTag('og:image', JURI::base(false).$pictures->{'image_intro'}, $type);
			$imgSet = 1;
		} else if (isset($pictures->{'image_fulltext'}) && $pictures->{'image_fulltext'} != '') {
			$this->renderTag('og:image', JURI::base(false).$pictures->{'image_fulltext'}, $type);
			$imgSet = 1;
		} else {
			// Try to find image in article
			
			$fulltext = '';
			if (isset($row->fulltext) && $row->fulltext != '') {
				$fulltext = $row->fulltext;
			}
			$introtext = '';
			if (isset($row->introtext) && $row->introtext != '') {
				$introtext = $row->introtext;
			}
			$content = $introtext . $fulltext;
			preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $src);
			if (isset($src[1]) && $src[1] != '') {
				
				$absU = 0;
				// Test if this link is absolute http:// then do not change it
				$pos1 			= strpos($src[1], 'http://');
				if ($pos1 === false) {
				} else {
					$absU = 1;
				}
				
				// Test if this link is absolute https:// then do not change it
				$pos2 			= strpos($src[1], 'https://');
				if ($pos2 === false) {
				} else {
					$absU = 1;
				}

				
				if ($absU == 1) {
					$linkImg = $src[1];
				} else {
					$linkImg = JURI::base(false).$src[1];
					if ($src[1][0] == '/') {
						$myURI = new \Joomla\Uri\Uri(JURI::base(false));
						$myURI->setPath($src[1]);
						$linkImg = $myURI->toString();
					} else {
						$linkImg = JURI::base(false).$src[1];
					}
				}
				
				$this->renderTag('og:image', $linkImg, $type);
				//$this->renderTag('og:image', JURI::base(false).$src[1], $type);
				$imgSet = 1;
			}
			
			// Try to find image in images/phocaopengraph folder
			if ($imgSet == 0) {
				if (isset($row->id) && (int)$row->id > 0) {
					
					jimport( 'joomla.filesystem.file' );
					$imgPath	= '';
					$path 		= JPATH_ROOT . '/images/phocaopengraph/';
					if (JFile::exists($path . '/' . (int)$row->id.'.jpg')) {
						$imgPath = JURI::base(false) . 'images/phocaopengraph/'.(int)$row->id.'.jpg';
					} else if (JFile::exists($path . '/' . (int)$row->id.'.png')) {
						$imgPath = JURI::base(false) . 'images/phocaopengraph/'.(int)$row->id.'.png';
					} else if (JFile::exists($path . '/' . (int)$row->id.'.gif')) {
						$imgPath = JURI::base(false) . 'images/phocaopengraph/'.(int)$row->id.'.gif';
					}
					
					if ($imgPath != '') {
						$this->renderTag('og:image', $imgPath, $type);
						$imgSet = 1;
					}
				}
			}
		}

		// If still image not set and parameter Image is set as last, then try to add the parameter image
		if ($imgSet == 0 && $this->params->get('image'.$suffix, '') != '' && $parameterImage == 0) {
			$this->renderTag('og:image', JURI::base(false).$this->params->get('image'.$suffix, ''), $type);
		}
		
		// END IMAGE

User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 42856
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Open Graph , set image ordering

Post by Jan » 29 Dec 2017, 00:28

Hi, yes, the ordering is following:
https://www.phoca.cz/documents/70-phoca ... open-graph

For setting the image from the folder as first, the condition needs to be changed and this condition should be moved to start. This must be modified and tested, unfortunately I cannot say if some code is right or not when not tested.

Jan
If you find Phoca extensions useful, please support the project

Post Reply