I installed the plugin http://wordpress.org/plugins/automatic-featured-image-posts/ but I am having a problemm, because the plugin doesn't remove the original image from the post content after creating the featured image. (and therefore when I don't add the featured image there appear 2 images on the post)
I tried adding this to the auto-featured-image.php
add_action('publish_post', 'eliminaroriginal');
and then
function eliminaroriginal(){ //update the post without image
$post_parent_id = $post->post_parent === 0 ? $post->ID :
$post->post_parent; $contenido = preg_replace("/[caption
.+?[/caption]|\< [img][^>][.]*>/i", "", $post->post_content,
1); $mipost = array(); $mipost['ID'] = $post_parent_id;
$mipost['post_content'] = $contenido; wp_update_post( $mipost );
}
but it didn't have any result.
Please help me, I don't know what should I do.
Thank you before hand!
Related
Need some help with images.
As the topic says, I want to create an image with live updating text on it.
This is mainly used by game servers and players to show off their stats and info. Server-wise, to show if the server is online and how many players are online and such.
I haven no idea how to do it and figured this would be the best place to find out. Since I don't have any idea what this is even called, i did not know what to search for.
Thanx in advance.
This is an html+css+ajax solution. Make the image an html element's background image. Let's use a div. Then position the text in the div. Use ajax to update the text. The image will remain the same while the text can be updated.
Was possible with the use of API. Created an API of the database I want to use, connected to the database and everything worked!
After using $API_GET to get the data, I set it into an array-format.
Used PHP for everything.
From there on I used imagecreatefrompng, obviously to select an image.
Set a few colors using imagecolorallocate and from there on it was purely adding the text on the image as to where I want them using imagettftext.
Creating an image, with live updating stats on it.
But we still have to tell the browser to load it as an image and not a page with an image on it, so therefor...
header( "Content-type: image/png" );
And imagesetthickness to 5, followed by imagepng and imagedestroy.
Code Extracts:
$API_GET = file_get_contents("API URL");
$API_GET = json_decode($API_GET);
$API = array();
foreach ($API_GET as $key => $value) {
$API[$key] = $value;
}
if (file_exists($imagefile)) {
$bg_img = imagecreatefrompng ( "IMAGE URL");
$color_red = imagecolorallocate( $bg_img, 250, 0, 0 );
imagettftext($bg_img, 20, 0, 19, 36, $color_red, 'FONT URL.tff',"Text On Image");
imagesetthickness ( $my_img, 5 );
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
} else {
echo 'Image does not exist.';
}
That is the basic structure of the code.
Thank You for your reply to the question.
I found this: http://docs.magentocommerce.com/Varien/Varien_Image/Varien_Image.html#crop
But I'm not sure if this is deprecated or something because when I tried this:
echo rawurlencode($this->helper('catalog/image')->init($_product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->setWatermarkImageOpacity(0)->crop(10, 20, 30, 40)->resize(300, null))
It doesn't work and gives me this error:
Fatal error: Call to undefined method Mage_Catalog_Helper_Image::crop() in /home/xxxxx/public_html/app/design/frontend/default/xxxxx/template/catalog/product/view.phtml
So is the crop() method actually usable at all? If it is, how can I use it to crop (not to be confused with resize) the product images of Magento? Thanks!
Your mistake is assuming that $this->helper('catalog/image')->init($_product, 'image') returns a Varien_Image instance, when in fact there are two intermediate classes involved:
Mage_Catalog_Helper_Image and Mage_Catalog_Model_Product_Image.
The catalog/image helper is a mess, even though it has been cleaned up a bit in recent versions (e.g. no more private methods). Still, some getters still are protected without there being a real need for it.
Here is my workaround:
/* #var $imageHelper Mage_Catalog_Helper_Image */
// Initialize the image helper
$imageHelper = Mage::helper('catalog/image')->init($_product, 'image')
->constrainOnly(true)
->keepAspectRatio(true)
->keepFrame(false)
->setWatermarkImageOpacity(0);
// Get the catalog/product_image instance
/* #var $imageModel Mage_Catalog_Model_Product_Image */
$reflection = new ReflectionClass($imageHelper);
$property = $reflection->getProperty('_model');
$property->setAccessible(true);
$imageModel = $property->getValue($imageHelper);
// Initialize the missing values on the image model
// Usually done in Mage_Catalog_Helper_Image::__toString()
if (! $imageModel->isCached())
{
$getWatermarkMethod = $reflection->getMethod('getWatermark');
$getWatermarkMethod->setAccessible(true);
$imageModel->setBaseFile($_product->getImage())
->resize()
->setWatermark($getWatermarkMethod->invoke($imageHelper));
// Crop the image using the image processor
// $imageModel->getImageProcessor() returns a Varien_Image instance
$imageModel->getImageProcessor()->crop(10, 20, 30, 40);
// Generate the image according to the set parameters and
// get the URL while bypassing the helper to avoid reinitialization
$url = $imageModel->saveFile()->getUrl();
}
echo $url . "\n";
It would be easier to use the catalog/product_image model or Varien_Image directly, but this way all the Magento watermark settings still are applied.
Either way isn't clean.
I hope the getters on the helper are made public in future releases.
Here is the alternative method ( credits from Onlinebizsoft.com )
The following code first looking the image that available in the /resize directory, if it's not there, doing rest of things.
// actual path of image
$_imageUrl = Mage::getBaseDir('media').DS."myimage".DS.$post->getThumbnail();
// path of the resized image to be saved
// here, the resized image is saved in media/resized folder
$imageResized = Mage::getBaseDir('media').DS."myimage".DS."resized".DS.$post->getThumbnail();
// resize image only if the image file exists and the resized image file doesn't exist
// the image is resized proportionally with the width/height 135px
if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
$imageObj = new Varien_Image($_imageUrl);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->resize(135, 135);
$imageObj->save($imageResized);
endif;
Check the website Resize - Scale Crop images
Did you tried Varien_Image class ?
$image = new Varien_Image($img);
$cropped = $image->crop();
I'm trying to do something like in this Tutorial, a very basic gallery.
In the example of the Tut they load images from uploads/media/ like so
page.10.marks.PROJECTTHUMBNAIL = IMG_RESOURCE
page.10.marks.PROJECTTHUMBNAIL {
stdWrap.wrap = <img src="|" />
file {
import = uploads/media/
import.data = levelmedia: -1,slide
import.listNum = 0
}
}
but now I want to load pictures that have been uploaded in an image-cObject.
This is an embarrassing question but I've been trying to figure this out for two days and I can't seem to get it right -.- I'm sure there are lots of answers out there... I just don't know the magic words to put into google to FIND them T-T
I tried very basic stuff like just doing the same as above but with a different path, I rummaged through the TSRef of IMAGE and IMG_RESOURCE, tried fiddling with CONTENT, and tried to adapt the tt_content.image.20 = USER (?? O.o) description in the typoscript object-browser... but all to no avail, as I know so little what I'm doing -.-
Any nudge in the right direction would be greatly appreciated!
You have to load the content elements using the CONTENT cObject and set how the content shall be rendered. This will load Image content elements on the given page regardless of what column they are in:
page.10.marks.PROJECTTHUMBNAIL = CONTENT
page.10.marks.PROJECTTHUMBNAIL {
table = tt_content
select {
where = CType = 'image' AND image != ''
orderBy = sorting ASC
}
renderObj = IMAGE
renderObj {
file {
import = uploads/pics/
import.field = image
import.listNum = 0
}
}
}
NOTE: The renderObj is just my example and it renders only the first image of the Image element. You can set the rendering as you please, e.g. set the file to be GIFBUILDER which would allow you to resize the image. You can also tweak the select to load content elements with more refined conditions.
i am trying to display product gallery images in my custom cms home page. If i use the code from media.phtml to display the gallery images, it does not work. I found this piece of code and it worked.
<div id="thumbs" class = "thumbs-home">
<?php
$obj = new Mage_Catalog_Block_Product_View_Media();
$_product1 = new Mage_Catalog_Model_Product();
// Load all product information of a particular product
$Products_one = Mage::getModel('catalog/product')->load($productId);
// Use your Product Id instead of $id
$countt = count($Products_one->getMediaGalleryImages());
if($countt>0){
foreach ($Products_one->getMediaGalleryImages() as $_image)
{
// For the Original Image
$thumb_img = "<img src=".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"",$_image->url)." alt=''width='60' height='60' />";
echo "<a href='".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"",$_image->url)."'rel='lightbox[gallery]'>".$thumb_img."</a>";
//For gallery Image
//$resizeimage = $obj->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->backgroundColor(242,242,243)->resize(400,300);
//echo "<img src=".$resizeimage."alt='' />";
}
}
?>
This gets the actual images and gets resized by the width and height attributes. But i want to resize the image through magento. The last piece of code $resizeimage is not working for some reason. How can i make this work? The problem is that i am using a lightbox to display the gallery images which displays the actual high resolution images that are too large. The light box takes in width and height of image provided and i am not able to figure out as to how i set a standard dimensions for the light box. So the only other option is the have the images resized by magento before passing them to the lightbox. Thanks.
You just need to call proper object, instead of $obj->helper you should use Mage::helper, so your call should look like this:
print Mage::helper('catalog/image')
->init($product, 'thumbnail', $image->getFile())
->backgroundColor(255,255,255)
->resize(100,100);
And that's it! :)
just imagine your profile on facebook. when changing your avatar, you select a new picture, crop it and then safe it. after that, the new avatar is displayed in your profile.
on my website i want do provide the same service - which allready works.
but: i am not able to display the new and(!) cropped avatar right after creating it (with php). i can only display the new, but uncropped one. after refreshing my site, every thing works fine.
$("div#button2").click(function() {
//Thumbnail is created externally with php
//the name of the new cropped avatar is the same like the uncropped one
//i just change the picture, but the url is the same!
$.post("upload_thumbnail.php", {
bild: bild,
format: format,
x: x,
y: y,
w: w,
h: h
});
//delete the old avatar
$.post("upload_loeschen.php", {
bild: bildalt
});
//save the url of the new cropped avatar in the database
$.post("upload_update.php", {
bild: bild,
laden_nr: "1"
});
//close my imageeditor
$("div#bildeditor").hide();
jcrop_api.destroy();
$("#cropbox").attr("src", "");
$("#preview").attr("src", "");
//If there is a new avatar, display it!!! -> and here only the uncropped is displayed
if(bild != "") {
$("img#logo").attr("src", bild);
bild = "";
}
});
I just don't know how the site is able to display the uncropped avatar, because it isnt there any more. it was replaced by the cropped one.
Thank you very much!
Benjamin
the problem is that your avatar pic got cached, so try to add an random parameter with the image name
on load var i=1;
On avatar change i++;
and set the img src ="imgpath?random="+i