Snippets

This Mächler TYPO3 - Fix magic images

Created by This Mächler
<?php
/*
** This script copies /fileadmin/_processed_/* files in TYPO3 installations if they are not matching
** the src-attributes found in tt_content.bodytext (RTE content, magic images). Just looks for the
** first part of the file (without the 10-digit postfix before the file extension) in the filesystem
** and renames it if necessary.
** 
** My usecase was a TYPO3 6.2.15 instance, and lots of lost magic images thumbnails, not wanting to fix them all by hand. 
**
** You have to call the script with the GET-param dry=no to make it rename files, else it will just do a dry run and show 
** you what it will do. 
**
** Author: 
** maechler@mm-computing.ch
** http://mm-computing.ch
** 2015-10-29
*/

/****************************************************************/
/** setup your database connection to make the script work! **/
/****************************************************************/
define( 'DB_HOST', '' );
define( 'DB_USER', '' );
define( 'DB_PW', '' );
define( 'DB_NAME', '' );
/****************************************************************/
/****************************************************************/



define( 'FILE_REGEX_PATTERN', '(csm_[^\s]+)(_[a-z0-9]+\.jpg)' );
$processed_dirname = dirname(__FILE__)."/fileadmin/_processed_";
$dry_run = ( strcmp($_GET['dry'], 'no') != 0 );

echo ($dry_run ? 'Dry run...' : 'Sharp run!!').'<br/><br/>';
/* ------------------- get all '/fileadmin/_processed_/*.jpg' files from tt_content.bodytext ------------------- */

echo "image sources pointing to 'fileadmin/_processed_/*':<br/>".
	"---------------------------------------------------------<br/>";


$mysqli = new mysqli(DB_HOST, DB_USER, DB_PW );
$mysqli->select_db(DB_NAME);

$res = $mysqli->query("SELECT bodytext FROM tt_content WHERE bodytext LIKE '%fileadmin/_processed_/csm%'");

$imgsrcs = array();
while( $row = $res->fetch_row() ){
	preg_match_all(
		'/fileadmin\/_processed_\/'.FILE_REGEX_PATTERN.'"/U', $row[0], 
		$matches, PREG_SET_ORDER
	);
	foreach( $matches as $m ){
		$imgsrcs[] = array($m[1],$m[2]);
		echo $m[1].$m[2]."<br/>";
	}
}
$mysqli->close();



/* ------------------- find 'fileadmin/_processed_/*.jpg' files in file system ------------------- */

echo "<br/><br/>read directory items '/fileadmin/_processed_/*':<br/>".
	"---------------------------------------------------------<br/>";
	
$dir = dir( $processed_dirname );
$imgfiles = array();
while( $f = $dir->read() ){
	if( preg_match('/^'.FILE_REGEX_PATTERN.'$/', $f, $m) ){
		$imgfiles[] = array($m[1], $m[2]);
		echo $m[1].$m[2]."</br>";
	}
}
$dir->close();



/* ----------------------- copy 'fileadmin/_processed_/*.jpg' files to match src in html ------------ */

echo "<br/><br/>"."copy 'fileadmin/_processed_/*.jpg' files to match src in html<br/>".
	"---------------------------------------------------------<br/>";
	
function get_src_filenames($filenamepart1){
	global $imgsrcs;
	$res = array();
	foreach( $imgsrcs as $imgsrc ){
		if( $filenamepart1 == $imgsrc[0] ){
			$res[] = $imgsrc[0].$imgsrc[1];
		}
	}
	return $res; // no matching src found
}
	
foreach( $imgfiles as $imgf ){
	$existing_fn = $imgf[0].$imgf[1];
	$src_filenames = get_src_filenames($imgf[0]);
	foreach( $src_filenames as $src_fn ){
		$src_file_withpath = $processed_dirname.'/'.$src_fn;
		if( (strcmp($existing_fn, $src_fn)!=0) && (!file_exists($src_file_withpath)) ){
			// this src attribute has no corresponding file, copy the existing one
			if( !$dry_run ){
				copy($processed_dirname.'/'.$existing_fn, $src_file_withpath);
				echo '(!) ';
			}
			echo $existing_fn." ===> ".$src_fn."<br/>";
		}
	}
}

?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.