Jump to content

TCPDF: preg_replace with a function


interactive

Recommended Posts

Hi,

 

I'm using TCPDF to create a PDF from a webpage.

This works great but I have the following problem:

 

I'm building in WordPress and as you may know WP saves images on different sizes. (e.g. 150x150, 300x300 and 1024x1024)

The thing I'm trying to create is that users can download a PDF ready for printing. So High Resolution images.

I use a preg_match_all function to match the image source and remove the additional image size from the filename so the original high resolution image is loaded.

 

e.g. image-150x150.jpg will be replaced by image.jpg.

So now the original image that has a image size of 6024x6024px and 300 DPI is loaded.

Then TCPDF uses a resize function that resizes the image back to 150x150 px but sets the DPI to 300 so it can be printed on a good quality.

 

This all works but now the problem comes:

If I do the preg_match_all function the image gets "pulled" from the rest of the content and looses it's position.

So my thought to fix this was. Get a preg_replace function and replace the image tag with a function that executes the whole image change like described above.

 

This doesn't work......

 

So i'm stuck here. Hope anyone can assist me with this.

Here is what I have done so far.

function execute(){	
	preg_match_all("/<img[^>]+src=(?:\"|\')\K(.[^\">]+?)(?=\"|\')/",$content,$matches,PREG_PATTERN_ORDER);
		for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) 
		{
			$search = array('-'.$s1.'x'.$s2.'', '-'.$s3.'x'.$s4.'', '-'.$s5.'x'.$s6.'');
			$replace = array('', '', '');
			
			
			$a = $matches[1][$i];
	
			if (strpos($a,'-'.$s1.'x'.$s2.'') !== false) {
				$w = $s1;
				$w = $pdf->pixelsToUnits($w);
				$h = $s2;
				$h = $pdf->pixelsToUnits($h);
				$l = '57';
				$l = $pdf->pixelsToUnits($l);
				$t = '170';
				$t = $pdf->pixelsToUnits($t);
			}elseif (strpos($a,'-'.$s3.'x'.$s4.'') !== false) {
				$w = $s3;
				$w = $pdf->pixelsToUnits($w);
				$h = $s4;
				$h = $pdf->pixelsToUnits($h);
				$l = '217';
				$l = $pdf->pixelsToUnits($l);
				$t = '20';
				$t = $pdf->pixelsToUnits($t);
			}elseif (strpos($a,'-'.$s5.'x'.$s6.'') !== false) {
				$w = $s5;
				$w = $pdf->pixelsToUnits($w);
				$h = $s6;
				$h = $pdf->pixelsToUnits($h);
				$l = '57';
				$l = $pdf->pixelsToUnits($l);
				$t = '310';
				$t = $pdf->pixelsToUnits($t);
			}
			
			$content .= $pdf->Image(str_replace($search,$replace,$matches[1][$i]), $l, $t, $w, $h, 'JPG', '', '', true, 300, '', false, false, 0, false, false, false);
		}
	};			

global $post;
$id = $_GET['id'];
$content_post = get_post($id);// get the content
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);

$content = preg_replace('/<img[^>]+./e',execute(), $content); // replace the image tag with the new image

// EXPORT IT ALL
$pdf->writeHTML($content, true, false, true, false, '');
Link to comment
Share on other sites

Call me stupid but I just don't see it.

I used the preg_replace_callback but there is no image show:

 $content = preg_replace_callback('/<img[^>]+./',
        function () {
           preg_match_all("/<img[^>]+src=(?:\"|\')\K(.[^\">]+?)(?=\"|\')/",$content,$matches,PREG_PATTERN_ORDER);
				for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) 
				{
					$search = array('-'.$s1.'x'.$s2.'', '-'.$s3.'x'.$s4.'', '-'.$s5.'x'.$s6.'');
					$replace = array('', '', '');
					
					
					$a = $matches[1][$i];
			
					if (strpos($a,'-'.$s1.'x'.$s2.'') !== false) {
						$w = $s1;
						$w = $pdf->pixelsToUnits($w);
						$h = $s2;
						$h = $pdf->pixelsToUnits($h);
						$l = '57';
						$l = $pdf->pixelsToUnits($l);
						$t = '170';
						$t = $pdf->pixelsToUnits($t);
					}elseif (strpos($a,'-'.$s3.'x'.$s4.'') !== false) {
						$w = $s3;
						$w = $pdf->pixelsToUnits($w);
						$h = $s4;
						$h = $pdf->pixelsToUnits($h);
						$l = '217';
						$l = $pdf->pixelsToUnits($l);
						$t = '20';
						$t = $pdf->pixelsToUnits($t);
					}elseif (strpos($a,'-'.$s5.'x'.$s6.'') !== false) {
						$w = $s5;
						$w = $pdf->pixelsToUnits($w);
						$h = $s6;
						$h = $pdf->pixelsToUnits($h);
						$l = '57';
						$l = $pdf->pixelsToUnits($l);
						$t = '310';
						$t = $pdf->pixelsToUnits($t);
					}
					
					$content .= $pdf->Image(str_replace($search,$replace,$matches[1][$i]), $l, $t, $w, $h, 'JPG', '', '', true, 300, '', false, false, 0, false, false, false);
				}	
        },
        $content
    );
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.